首页 文章

使用nginx和php找不到文件

提问于
浏览
0

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

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

nginx | 2017/08/27 20:47:48 [错误] 6#6:* 6在stderr中发送FastCGI:“主脚本未知”,同时从上游读取响应头,客户端:172.23.0.1,服务器:localhost,请求:“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,和Gecko一样) )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;

正如一些线程中所建议的那样,但这不起作用

2 回答

  • 0

    因此,我试图在过去3天内处理您的问题,以便更好,更深入地了解FASTCGI . 我在NGINX和FPM之间放了一个 Logger ,这有助于我更好地理解这些交互 . 以下是我认为适合您的配置

    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

  • 0

    当前配置适用于我,但/ 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;
    }
    

相关问题