Running both PHP and Node.js on Nginx on the same server

Posted January 10, 2015 39.8k views

Is it possible to run both PHP and Node.js application on the same server with Nginx? I use nginx as reverse proxy for node.js while on PHP I use nginx as substitute for Apache web server.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
4 answers

Check this Gist… it works for me ;)

Practically you can copy and paste, just need to update the routes.

Hope this works for you!

This is definitely possible. It can be implemented by setting up separate server blocks for each app. Check out this tutorial:

At its most basic, you can create a separate block for each. This uses the server_name directive to route requests to to your php app and requests to to the node app running on port 2368.

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html/php;
    index index.php index.html index.htm;


    location / {
        try_files $uri $uri/ =404;

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

server {
    listen 80;
    listen [::]:80 ipv6only=on;


    root /usr/share/nginx/html/node;
    index index.html index.htm;

    client_max_body_size 10G;

    location / {
        proxy_pass http://localhost:2368;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;

If you need to do something a little more complicate, you might also want to check out:

by Justin Ellingwood
Nginx is an incredibly powerful web server and reverse proxy. In this guide, we'll discuss how to set up server blocks on Ubuntu 14.04 in order to server multiple domains from a single server.

Thanks! But I already figured it out 2 days ago. :)

I got error like this
Starting nginx: nginx: [emerg] duplicate listen options for [::]:80 in /var/www/hosts/librarian.conf:32