How to change the port of a nginx reverse proxy from 80 to 3001

August 3, 2018 1.2k views
Nginx Node.js Ubuntu 16.04

Hi,
my setup consists of a node App serving a Rest Api. This Api operates on localhost:3000. To publish it, i want to reverse proxy this api with the port 3001. How is this possible in nginx.

Here is my first try, that doesnt work.

server {
        listen 3001;
        listen [::]:3001;

        root /var/www/html;
        index index.html index.htm index.nginx-debian.html;

        server_name api.xxxxx.de;

         location / {
              proxy_pass http://localhost:3000;
              proxy_http_version 1.1;
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection 'upgrade';
              proxy_set_header Host $host;
              proxy_cache_bypass $http_upgrade;
              }

}

1 Answer

Hello,

You can open the nginx config file in your preferred text editor:

/etc/nginx/sites-enabled/default  -  On Ubuntu
/etc/nginx/nginx.conf             -  On CentOS

Search for the line that begins with listen statement in server directive and change the port from 80 to 3001, so it can looks like this:

server {
        listen 3001 default_server;
        listen [::]:3001 default_server;

        root /var/www/html;
        index index.php index.html index.htm;

        server_name _;

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

After altering Nginx port statement, you need to restart nginx in order to bind on the new port on Debian based Linux distributions. Verify local network sockets table with netstat or ss command. Port 3200 should be displayed in your server local network table.

systemctl restart nginx
netstat -tlpn| grep nginx
ss -tlpn| grep nginx

If you’re on CentOS and if SELinux is enabled you would need to install policycoreutils

Once this is done you should be able to access your site/app on port 3001

http://sever.ip:3001

I hope this helps!

Alex

Have another answer? Share your knowledge.