2017-08-27 55 views
0

我试着用nginx在同一个域上运行node js和php。然而,php(“/ ajax”)的位置不起作用。我总是收到消息“找不到文件”。 nginx的日志打印找不到nginx和php的文件

网址为http://localhost:8085/ajax到目前为止,脚本位于位于/ var/WWW /公共 文件夹/ AJAX不存在(所有路径都不做,因为寄托都将被重定向到/ var/www/public/index.php)

nginx | 2017/08/27 20:47:48 [error] 6#6:* 6当读取来自上游的响应头时,stderr发送的FastCGI:“主要脚本未知”,客户端: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,like 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 /公共$ fastcgi_script_name;

的建议在某些线程,但不工作

+0

后,您访问过的网址,并在文件夹中的文件应该有一部分它。另外请记住,阿贾克斯去在该网址的路径,所以如果您的文件夹没有Ajax的脚本运行将不会被发现,因此错误 –

+0

感谢您的建议。我更新了我的帖子 – Sebastian

回答

0

所以我想你的问题的工作最近3天,以获得更好的和更深入的了解FASTCGI的。我在NGINX和FPM之间放置了一个记录器,这有助于我更好地理解这些交互。下面是我认为应该工作的配置为你

编辑-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; 
} 

所以基本思想是利用从document_root更换/ajaxalias指令。然后覆盖request_uri和其他参数,以便正确的URL到达路由解析的PHP代码。

下面是什么我的PHP脚本接收当我打电话http://vm/ajax/router.php?x=2

{ 
    "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

哇。非常感谢。我将尝试该解决方案并提供反馈 – Sebastian

+0

不幸的是,它无法正常工作。 http:// localhost:8085/ajax/search?q = 1显示“此页面不工作localhost没有发送任何数据。”和http:// localhost:8085/ajax显示“403 Forbidden”。它甚至没有达到index.php(端口映射) – Sebastian

+0

我刚刚复制你的 – Sebastian

0

当前配置对我的作品,但/ AJAX传递给PHP-FPM。我是不是能够在所有变量摆脱/ AJAX前缀像REQUEST_URI

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; 
}