使用单个nginx服务器来服务/代理PHP,Python和NodeJS

我想弄清楚如何使用Nginx作为代理服务PHP(通过PHP5-FPM),Python(通过gunicorn)和NodeJS。 我在网站可用目录中的当前默认文件复制如下。 我应该尝试configuration多个服务器还是进行其他更改才能启用此function? 提前致谢。

更新:目前,使用当前的configuration,Nginx作为NodeJS应用程序的代理。 但是,它不再提供PHP内容。 我应该在默认文件中使用不同的服务器吗?如果是这样,我应该能够使用相同的侦听端口,但只是使用不同的server_name,并使用位置标记来区分请求?

我试图将某些URL请求路由到一个PHP应用程序(在/ var / www – 我从/ usr / share / nginx切换)以及Python和Nodejs后端。

有一个想法,我没有实现是尝试多个上游,并在主服务器的PHP设置 – 这将工作,即有一个上游的NodeJS,一个用于Python,然后是PHP的服务器。

upstream test { server 0.0.0.0:3002; keepalive 500; } server { listen 81 default_server; listen [::]:81 default_server; ##remove this? root /var/www/; ##switched from /usr/share/nginx index index.php index.html index.htm; server_name localhost; location / { proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-Nginx-Proxy true; proxy_set_header Connection ""; proxy_http_version 1.1; proxy_pass http://0.0.0.0:3002; } location /doc/ { alias /usr/share/doc/; autoindex on; allow 127.0.0.1; allow ::1; deny all; } # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests location /RequestDenied { proxy_pass http://127.0.0.1:4242; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; # fastcgi_pass 127.0.0.1:9000; # # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } } # another virtual host using mix of IP-, name-, and port-based configuration # server { listen 82; root /var/www/; index index.php index.html index.htm; server_name php; location ~ /testPHP { //testPHP is part of URL/directory name in /var/www/ fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } } 

不知道这是否是最好的方法,但它帮助我实现我想要的。 我只是为代理创build了一个新的服务器设置,并使用一台服务器来提供php内容。

 upstream test { server 0.0.0.0:3002; keepalive 500; } server { listen 81 default_server; listen [::]:81 default_server; ##remove this? root /var/www/; ##switched from /usr/share/nginx index index.php index.html index.htm; server_name localhost; location / { proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-Nginx-Proxy true; proxy_set_header Connection ""; proxy_http_version 1.1; proxy_pass http://0.0.0.0:3002; } location /doc/ { alias /usr/share/doc/; autoindex on; allow 127.0.0.1; allow ::1; deny all; } # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests location /RequestDenied { proxy_pass http://127.0.0.1:4242; } } # another virtual host using mix of IP-, name-, and port-based configuration # server { listen 82; root /var/www/; index index.php index.html index.htm; server_name php; location / { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } } 

我正在寻找一个类似的解决scheme,使用Nginx中的FastCGI和NodeJS服务器来提供PHP。 不过,我想提供来自同一个域和端口的所有请求。 我解决了基于位置的代理请求,而不是有一个单独的服务器。

这个例子使用上游:

 upstream nodejs { server 127.0.0.1:8080 max_fails=0; } server { listen 80; server_name _; set $root /var/www/sitename; root $root; index.php index index.html; access_log /var/www/sitename/logs/access.log; error_log /var/www/sitename/logs/error.log; location ~ \.php$ { fastcgi_pass 127.0.0.1:8888; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $root/$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name; include /etc/nginx/fastcgi_params; if (!-e $request_filename) { rewrite ^(.+)$ /index.php?q=$1 last; break; } } location /nodejs/ { proxy_pass http://nodejs; proxy_redirect off; proxy_set_header Host $host ; proxy_set_header X-Real-IP $remote_addr ; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; } } 

我发现这写得很有用,这个代码更不是一个克隆:

http://blog.i-evaluation.com/2013/04/05/lannn-setting-up-nginx-with-php-and-node-js-on-aws/