Nginx将$ fastcgi_script_name与$ fastcgi_path_info混淆

时间:2018-07-10 13:00:41

标签: url nginx configuration configuration-files fastcgi

这是我的Nginx配置文件的相关部分:

http {
    log_format  fastcgi
                '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$fastcgi_script_name" "$fastcgi_path_info"';

    server {
        listen       8080;
        server_name  localhost;

        access_log  /usr/local/var/log/nginx/access.log  fastcgi;

        location / {
            fastcgi_index /;
            fastcgi_pass unix:/usr/local/var/www/run/httpd.sock;
            include fastcgi_params;
        }
    }
}

基本上,我在/usr/local/var/www/run/httpd.sock有一个Unix套接字,可以处理FastCGI请求,并且工作得很好。这里的问题是Nginx认为URI的最后一部分是脚本名称,但它应该是路径信息。例如

⇒ nginx -v
nginx version: nginx/1.15.0
⇒ nginx
⇒ curl -i localhost:8080/index
HTTP/1.1 200 OK
Server: nginx/1.15.0
Date: Tue, 10 Jul 2018 12:27:15 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive

<p>hello, world</p>
⇒ tail -n 1 /usr/local/var/log/nginx/access.log
127.0.0.1 - - [10/Jul/2018:20:27:15 +0800] "GET /index HTTP/1.1" 200 30 "-" "/index" ""

这意味着$fastcgi_script_name/index,其中$fastcgi_script_name是一个空字符串。

如何配置Nginx使$fastcgi_script_name包含URI的最后部分,例如/index之类的东西?

1 个答案:

答案 0 :(得分:0)

正如@Richard Smith在其评论中指出的那样,您必须通过配置fastcgi_split_path_info来解析路径信息。

例如,在这种情况下,您会想要

location / {
    fastcgi_split_path_info  ^()(.*)$;
    fastcgi_pass  unix:/usr/local/var/www/run/httpd.sock;
    include  fastcgi_params;
}