Change Nginx HTTP Server Port on Ubuntu 16.04 | 17.10 | 18.04

Nginx, probable the second most popular HTTP server in use today is easy to manage and configure… Nginx is a lightweight and efficient HTTP server, but can be served as a proxy server and more.

Sometimes you’ll want Nginx to listen and communicate over not its default port, but a different port instead… when you want to do that, the steps below can be a good starting point..

For example, if you want Nginx HTTP server to sit behind a proxy server, then the proxy server must be configured to listen on the default port 80… in this case, Nginx must also be configured to listen on a different port then 80…, since two services can’t be assigned one port to listen on… there would be contentions..

This tutorial is going to show students and new users how to easily switch Nginx default port from 80 to something else… like 8082.. in this way, other services can use port 80 to communicate..

When you’re ready to make this change, continue below:

Step 1: Identifying Nginx Port Config File

To change Nginx default port number, you need to look in single directory, unlike Apache2… this is Nginx default virtual hosts directory¬†/etc/nginx/sites-available... In this directory is where you’ll find individual virtual host configuration files…

Each of the file contains a port for Nginx to listen and communicate over… if you want to change Nginx port, you’ll do it in each of the virtual host file.

Below is the location to can change Nginx default port numbers

sudo nano /etc/nginx/sites-available/default

Step 2: Changing Nginx Port Number

Now that you’ve identified the files you can change Nginx port number, continue below to begin making those changes… for this post, we’ll change the default port 80 to 8082 and port 443 to 444.

First, run the commands below to open the port.conf file…

sudo nano /etc/nginx/sites-available/default

Then change the Listen line from 80 to 8082

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

        listen [::]:444 ssl;
        ssl_certificate /path_to_cert_file;
        ssl_certificate_key /path_to_cert_key_file; 

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

        server_name _;

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

Step 3: Restart Nginx

Now that you’ve changed the port number in all the files, run the commands below to restart Nginx..

sudo systemctl restart nginx.service

After that, Nginx service will begin listening on the port you assigned above…

That’s it! This is how one changes Nginx port numbers.

You may also like the post below:

The Best WordPress Nginx FastCGI Settings on Ubuntu 16.04 LTS Servers with PHP-FPM 7.1 Support

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.