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

}

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.

×
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

Submit an Answer