nginx 子目录匹配不同地方的文件夹

Fork Me On Github
zodream 编程技术 2019年11月

nginx 子目录匹配不同地方的文件夹

要求

/ 对应 /data/www

/shop 对应 /home/www/shop

/task 对应 /home/task1

/shop/h5 对应 /data/www/shop/h5

/bbs/index.php 对应 /data/bbs/index.php

第一个

                                                          
server
{
    listen       80 default;
    server_name  zodream.cn;
    rewrite ^(.*)$ https://${server_name}$1 permanent; # 强制使用https 访问
}

server
{
    listen       443 ssl;
    server_name  zodream.cn;
    index index.html index.htm index.php;
    root  /data/www;
    ssl_certificate /data/ssl/zodream.cn.pem;
    ssl_certificate_key /data/ssl/zodream.cn.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ALL:!DH:!EXPORT:!RC4:+HIGH:+MEDIUM:!LOW:!aNULL:!eNULL;

    location / {
    }

    location ~ ^/shop/h5/.+\.php {
        root /data/www;
        include php_fcgi.conf;
    }


    location ~ ^/shop/h5.* {
        root /data/www;
    }

    location /shop {
        root /home/www; # 会访问 /home/www/shop 文件下的所有html文件,不能访问其他类型文件
    }

    location ~ ^/shop {
        root /home/www; # 会访问 /home/www/shop 文件下的所有类型的文件
    }


    location ~ ^/task.* {
        root /home/task1;
        rewrite ^/task(.*)$ /$1 break;    # 这种方法跟上一种方法效果一样但不需要保持文件名一致
    }

    location ~ ^/bbs/.*\.php.* {       # 此方法存在一个问题即默认的 /bbs php程序提示找不到文件   
        root /data/bbs/;
        include php_fcgi.conf;
        set $real_script_name $fastcgi_script_name;
        if ($fastcgi_script_name ~ "^/bbs/(.+?\.php)(.*)$") {
            set $real_script_name $1;
            set $path_info $2;
        }
        fastcgi_param SCRIPT_FILENAME /data/bbs/$real_script_name;
        fastcgi_param SCRIPT_NAME $real_script_name;
        fastcgi_param PATH_INFO $path_info;
    }
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758

php_fcgi.conf

                            
fastcgi_pass  127.0.0.1:9000;
fastcgi_index index.php;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

12345678910111213141516171819202122232425262728
点击查看全文
0 1637 0