我正在尝试找出如何最好地使用 Nginx 作为代理来为 PHP(通过 PHP5-FPM)、Python(通过 gunicorn)和 NodeJS 提供服务。我当前在 sites-available 目录中的默认文件已复制到下面。我是否应该尝试配置多个服务器或进行其他更改以启用此功能?提前致谢。
更新: 目前,在当前配置下,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;
}
}
答案1
不确定这是否是最好的方法,但它帮助我实现了我想要的。我只是为代理创建了一个新的服务器设置,并使用一台服务器来提供 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;
}
}
答案2
我正在寻找一个类似的解决方案,使用 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/