【问题标题】:Nginx confusing $fastcgi_script_name with $fastcgi_path_infoNginx 混淆 $fastcgi_script_name 和 $fastcgi_path_info
【发布时间】:2018-12-18 08:13:03
【问题描述】:

这是我的 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 这样的东西?

【问题讨论】:

  • $fastcgi_path_info 使用fastcgi_split_path_info 指令设置(请参阅this link)。默认情况下,$fastcgi_script_name$uri 相同。

标签: url nginx configuration configuration-files fastcgi


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 2012-05-24
    • 2016-07-11
    • 2021-04-10
    • 2021-11-06
    • 2011-12-04
    • 2016-01-12
    相关资源
    最近更新 更多