nginx 配置多个 root 不起作用

nginx 配置多个 root 不起作用

根据配置,site.com应该html/web/index.php默认打开,但是site.com/ticket应该打开html/ticket/index.phpticketweb文件夹都位于html

server {
    listen 80;
    return 301 https://$host$request_uri;
}

server {

add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;

    server_name  site.com;
    server_tokens off;

    root /usr/share/nginx/html/web;

    location / {
        index index.php;
    }

    error_page   404              /404.html;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include         fastcgi_params;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny  all;
    }

    location /ticket {
            root /usr/share/nginx/html/ticket;
            index index.php index.html index.htm;
    }
}

它打开了web/index.php,但是/ticket它说Not found

更新

上面看起来/usr/share/nginx/html/ticket/ticket/index.php

我也试过了

location /ticket {
    root /usr/share/nginx/html;
    index index.php index.html index.htm;
}

location /ticket {
    alias /usr/share/nginx/html/ticket;
    index index.php index.html index.htm;
}

两种配置都看起来如此/usr/share/nginx/html/web/ticket/index.php

它应该看起来/usr/share/nginx/html/ticket/index.php

我认为问题出在 FastCGI

解决方案

问题出在 FastCGI 配置上

        try_files  $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;

答案1

这会起作用。我已经测试过了。

server {

    server_name  site.com;
     server_tokens off;
    root /usr/share/nginx/html/web; 

   location / {
        index index.php;
    }

   location /ticket  {
        index index.php;
        alias /usr/share/nginx/html/ticket/;


    if (-f $request_filename) {
        break;
    }


    if (-d $request_filename) {
        break;
    }

    rewrite (.*) /ticket/index.php?$query_string;        

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

        }



    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }


}

答案2

您需要使用alias指令。nginx 不会alias像 那样在 指定的目录后添加完整的规范化 URI root

因此,你的区块看起来将是这样的:

location /ticket {
    alias /usr/share/nginx/html/ticket;
    index index.php index.html index.html;
}

答案3

请参阅文档root指示。在里面location,它仍然是相对于根的,而不是相对于那个特定位置的。

设置请求的根目录。例如,使用以下配置

location /i/ {
    root /data/w3;
}

/data/w3/i/top.gif文件将根据 /i/top.gif请求发送。

因此,您应该:

location /ticket/ {
    root /usr/share/nginx/html;
    index index.php index.html index.htm;
}

否则,它将尝试访问/usr/share/nginx/html/ticket/ticket/index.php

相关内容