Skip to content

Instantly share code, notes, and snippets.

@chrnx-dev
Last active December 28, 2015 22:09
Show Gist options
  • Save chrnx-dev/7569928 to your computer and use it in GitHub Desktop.
Save chrnx-dev/7569928 to your computer and use it in GitHub Desktop.
Proxy NodeJS app to Nginx Proxy

###After Install Nginx

After Install Nginx need to configure the files to add proxy to nginx

cd /etc/nginx/sites-available/
vim test

In the file you need to paste the next code.

# Node App Staging on Port 3000
upstream example{
    server 127.0.0.1:3000;
}

# NGINX Server Instance
server {
    listen 0.0.0.0:80;
    server_name example.com;
    access_log /var/log/nginx/example.com.log;

    # Gzip Compression
    gzip on;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_min_length  1000;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_buffers 16 8k;

    # Proxy to the Node instance
    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_pass http://example.com;
      proxy_redirect off;
    }
 }

Save the file and make a symbolic link to activate the new configuration.

ln -s /etc/nginx/sites-available/test /etc/nginx/sites-enabled/test
/etc/init.d/nginx reload

If you don't have a DNS server need to modify the hosts file.

sudo vim /etc/hosts

and adds the next line

127.0.0.1    example.com

Now you can open in your browser http://example.com/ and now you can see your app using the name that you select it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment