Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am trying to figure out how best to use Nginx as proxy for serving PHP (via PHP5-FPM), Python (via gunicorn) and NodeJS. My current default file in the sites-available directory is copied below. Should I be attempting to configure multiple servers or make other changes in order to enable this functionality? Thanks in advance.

Update: Currently, with the current config, Nginx is serving as a proxy to NodeJS application. However, it is no longer serving PHP content anymore. Should I be using a different server in the default file and if so, should I be able to use the same listening port but just use a different server_name and use the location tag to differentiate between the requests?

I am trying to route certain URL requests to a PHP application (in /var/www - I switched from /usr/share/nginx) as well as to Python and Nodejs backends.

One thought that I have not implemented is to try multiple upstream and have the PHP setup in the main server - would that work i.e. have one upstream for NodeJS, one for Python and then the server for 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;
        }

}
share|improve this question
    
What does that mean? "it is no longer serving PHP content anymore"? –  Michael Hampton Dec 12 '13 at 19:13

2 Answers 2

Not sure if this was the best way of approaching but it helped me achieve what I wanted. I simply created a new server setup for the proxy and used one server for serving php content.

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

} 
share|improve this answer

I was looking for a similar solution, serving PHP using FastCGI in Nginx along with a NodeJS server. However I wanted serve all requests from the same domain and port. I settled for proxying requests based on location instead of having a separate servers.

This example uses upstream:

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

I found this write up useful and this code is more a less a clone:

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.