【问题标题】:File not found with nginx and php使用 nginx 和 php 找不到文件
【发布时间】:2018-02-05 03:31:06
【问题描述】:

我尝试使用 nginx 在同一个域上运行节点 js 和 php。但是 php ("/ajax") 的位置不起作用。我总是收到消息“找不到文件。”。 nginx 日志打印

到目前为止,URL 是 http://localhost:8085/ajax,脚本位于 /var/www/public 文件夹 /ajax 不存在(所有路径都不存在,因为一切都应重定向到 /var/www/public/index.php)

nginx | 2017/08/27 20:47:48 [error] 6#6: *6 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.23.0.1, server: localhost, request: "GET /ajax HTTP/1.1”,上游:“fastcgi://172.23.0.4:9000”,主机:“localhost:8085”

nginx | 172.23.0.1 - - [27/Aug/2017:20:47:48 +0000] "GET /ajax HTTP/1.1" 404 27 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, 像壁虎) Chrome/60.0.3112.113 Safari/537.36" "-"

这是我的配置,我要改什么?

upstream react {
    server react:3000;
    keepalive 8;
}

server {
    listen 0.0.0.0:80;

    server_name  localhost;

    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://react;
        proxy_redirect off;
    }

    location /ajax {
        index index.php index.html;
        root /var/www/public;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    client_max_body_size 5m;
}

我试过了

fastcgi_param SCRIPT_FILENAME /var/www/public$fastcgi_script_name;

在某些线程中建议,但这不起作用

【问题讨论】:

  • 发布您访问过的 URL 以及该文件应包含的文件夹。还要记住,带有 go 作为该 url 中的路径的 ajax,因此如果您的文件夹没有 ajax,则将找不到要运行的脚本,因此会出现错误
  • 感谢您的建议。我更新了我的帖子

标签: php nginx


【解决方案1】:

当前配置对我有用,但是 /ajax 被传递给 php-fpm。我无法摆脱 REQUEST_URI 等所有变量中的 /ajax 前缀

upstream react {
    server react:3000;
    keepalive 8;
}

server {
    listen 0.0.0.0:80;

    server_name localhost;

    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://react;
        proxy_redirect off;
    }

    location /ajax {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /var/www/public$fastcgi_script_name;
        fastcgi_param QUERY_STRING    $query_string;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    client_max_body_size 5m;
}

【讨论】:

    【解决方案2】:

    因此,过去 3 天我一直在努力解决您的问题,以更好、更深入地了解 FASTCGI。我在 NGINX 和 FPM 之间放置了一个记录器,这有助于我更好地理解交互。以下是我认为应该适合您的配置

    Edit-1

    解决聊天问题后更新配置

    upstream react {
        server react:3000;
        keepalive 8;
    }
    
    map $fastcgi_script_name $fastcgi_script_name_fcgi {
        "~*/ajax/(?P<rest_url>.*)$"   /$rest_url;
        default $fastcgi_script_name;
    }
    
    map $request_uri $request_uri_fcgi {
        "~*/ajax/(?P<rest_url>.*)$"   /$rest_url;
        default $request_uri;
    }
    
    map $document_uri $document_uri_fcgi {
        "~*/ajax/(?P<rest_url>.*)$"   /$rest_url;
        default $document_uri_fcgi;
    }
    
    server {
        listen 0.0.0.0:80;
    
        server_name localhost;
    
        location / {
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
    
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
    
            proxy_pass http://react;
            proxy_redirect off;
        }
    
        location /ajax {
            alias /var/www/public;
            try_files $uri @php;
        }
    
        location @php {
            fastcgi_split_path_info ^/ajax/(.+\.php)(/.+)$;
            fastcgi_pass fpm:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param  SCRIPT_NAME        /index.php;
            fastcgi_param  REQUEST_URI        $request_uri_fcgi;
            fastcgi_param  DOCUMENT_URI       $document_uri_fcgi;
            fastcgi_param  SCRIPT_FILENAME    /var/www/public/index.php;
        }
    
        client_max_body_size 5m;
    }
    

    所以基本思想是使用alias 指令从document_root 替换/ajax。然后覆盖 request_uri 和其他参数,以便正确的 url 到达 PHP 代码以进行路由解析。

    以下是我调用 http://vm/ajax/router.php?x=2 时我的 php 脚本收到的部分内容

    {
      "COOKIES": null,
      "GET": {
        "x": "2"
      },
      "POST": [],
      "REQUEST": {
        "x": "2"
      },
      "HEADERS": null,
      "SERVER": {
        "HOME": "/var/www",
        "SCRIPT_FILENAME": "/var/www/html/router.php",
        "DOCUMENT_ROOT": "/var/www/html",
        "DOCUMENT_URI": "/router.php",
        "REQUEST_URI": "/router.php?x=2",
        "SCRIPT_NAME": "/router.php",
        "CONTENT_LENGTH": "",
        "CONTENT_TYPE": "",
        "REQUEST_METHOD": "GET",
        "QUERY_STRING": "x=2",
        "FCGI_ROLE": "RESPONDER",
        "PHP_SELF": "/router.php",
        "argv": [
          "x=2"
        ],
        "argc": 1
      }
    }
    

    如你所见,/ajax

    【讨论】:

    • 哇。非常感谢。我将尝试该解决方案并提供反馈
    • 不幸的是它不起作用。 localhost:8085/ajax/search?q=1 显示“此页面无法正常工作 localhost 未发送任何数据。” localhost:8085/ajax 显示“403 Forbidden”。它甚至没有到达 index.php(端口已映射)
    • 我刚刚复制了你的
    • 好吧,您的示例中没有 /var/www/html
    猜你喜欢
    • 2017-09-05
    • 1970-01-01
    • 2014-08-04
    • 2016-06-30
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多