使用nginx作为php和nodejs的代理

时间:2013-12-12 17:24:09

标签: php nginx

除了通过Nginx(使用PHP5-FPM)提供PHP内容之外,我还希望使用Nginx作为Nodejs应用程序的代理。当代理设置不到位时,我最初能够提供PHP服务。但是,现在当我使用代理时,PHP设置不再有效。我应该在PHP的服务器设置(在sites-available / default下)指定不同的位置,还是在默认文件中使用单独的服务器。我目前在默认文件中有以下设置:

 upstream test {
        server 0.0.0.0:3002;
        keepalive 500;
}


server {
        listen 81 default_server;  
        listen [::]:81 default_server; #remove?

        root /var/www/;             #root /usr/share/nginx/html;
        #index index.php index.html index.htm;

        server_name localhost;

        location ~ /PHPApp {    ##url will include PHPApp
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

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

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
}

1 个答案:

答案 0 :(得分:0)

导频错误 - 我能够为多个后端服务器(例如NodeJS,Python等)使用上游选项,并使用多个服务器指令指向那些后端服务器。我使用PHP的原始服务器指令,并能够将流量路由到PHP,NodeJS和Python后端。

例如:

upstream node {
        server 0.0.0.0:3002; ##varnish -> back end app server
        keepalive 500;
}

upstream python {
        server 0.0.0.0:8080;
        keepalive 500;
}


server {
        listen 81 default_server;  ## haproxy is using port 80 - nginx switched to 81
        listen [::]:81 default_server; #ipv6only=on;

        root /var/www/;  #root /usr/share/nginx/html;
        index 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://node;
        }

}


# 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 ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

}


server {
        listen 83;
        root /var/www/;
        index index.html index.htm;
        server_name python;

        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://python;
        }
}