How to Change Nginx Port in Linux?


Introduction

Nginx is a popular open-source web server that is known for its speed, reliability, and scalability. The default port for Nginx is 80, which allows web traffic to flow in and out of the server. While this port works well in most cases, it can also make the server vulnerable to cyber attacks since it is a well-known port that hackers target.

Checking Current Nginx Configuration

Step-by-Step Guide on How to Check Current Nginx Configuration

Before changing the Nginx port, it is important to ensure that the current configuration is correct. To check the current Nginx configuration, follow these steps −

  • Log in to Linux server as a root or a user with sudo privileges.

  • Open the Nginx configuration file in your favorite text editor. The location of this file can vary depending on your operating system and installation method. In most cases, it is located at /etc/nginx/nginx.conf.

  • Check for any syntax errors in the configuration file by running the following command −

sudo nginx -t  

If there are no syntax errors, you should see an output message similar to "nginx: configuration file /etc/nginx/nginx.conf test is successful". If there are any errors, they will be displayed on your terminal.

Explanation of the Configuration File Location and Content

The Nginx configuration file contains all settings related to how Nginx operates on your server. The location of this file may vary depending on your operating system and installation method. In general, this file can be found at /etc/nginx/nginx.conf.

This is where you can specify settings such as ports, server names, SSL certificates and more. The structure of the configuration file consists of various blocks that are enclosed in curly braces {}.

Each block has a specific purpose such as defining HTTP servers or setting up SSL configurations. It is important to have a good understanding of the content within this configuration file before making any changes so you don't accidentally break something essential for your server's smooth functioning.

Changing Nginx Port in Linux

Step-by-step Guide on how to Change Nginx port in Linux

Changing the default port of Nginx in Linux is a simple process that requires some modifications to the configuration file. The default port number for Nginx is 80, but this can be changed to any available port number. To begin, open the configuration file with a text editor using the following command −

sudo nano /etc/nginx/nginx.conf  

Once you have opened the configuration file, locate the line that specifies the default port number, which should look like this −

listen 80;  

Change the "80" to your desired new port number.

For example, if you want to change it to 8080, modify the line as follows −

listen 8080;  

Explanation of Necessary Changes to be Made in the Configuration File

After modifying the "listen" directive in your NGINX server block files and restarting your NGINX server service daemon using systemctl or service commands will apply all changes made. In addition to changing "listen" directive and restarting NGINX server service daemon there are also other directives related with ports such as `server_name`, `proxy_pass` etc. These directives needs also update according to new settings. It's important to note that after changing your Nginx port, you may need to update other services or applications that interact with it.

For example, if you have a firewall configured on your system, you will need to allow traffic through your newly defined port. Furthermore, any DNS records pointing towards your old IP address may need updating if they specify a specific port number.

Testing the New Port Configuration

After making changes to the Nginx configuration file, it is important to test the new port configuration to ensure that it is working properly before restarting the Nginx service.

Step-by-step Guide on How to Test the New Port Configuration

The easiest way to test the new port configuration is by using telnet or curl command in your terminal. To test if your website is accessible through a browser, just type in your domain name followed by a colon and then a new port number (e.g. http://www.example.com:8080).

To test using telnet −

  • Type "telnet localhost NEW_PORT_NUMBER" with NEW_PORT_NUMBER being the new port you set in your Nginx configuration file.

  • If successful, you will see a response similar to "Trying 127.0.0.1…" followed by "Connected to localhost". If unsuccessful, you will see an error message.

To test using curl −

  • Type "curl http://localhost:NEW_PORT_NUMBER" with NEW_PORT_NUMBER being the new port you set in your Nginx configuration file.

  • If successful, you will receive a response similar to what you would typically receive when accessing your website via a web browser. If unsuccessful, you will see an error message.

Explanation of Common Issues That May Arise During Testing

There are several common issues that may arise during testing of the new port configuration −

  • Firewall blocking access − If there is a firewall on your server or network, it may be blocking access to your website on the new port. Make sure to update your firewall rules to allow access to the new port.

  • Incorrect port number − double-check that you have correctly added the new port number in your nginx configuration file.

  • Misconfigured Nginx − check for any syntax errors or typos in your nginx configuration file, as this could prevent nginx from starting up or serving content properly.

By following these steps and addressing any common issues that may arise, you can successfully change the Nginx port in Linux and ensure that your website remains accessible through the new port.

Additional Tips and Tricks

Using Firewall Rules to Restrict Access to New Port

One of the most important steps you can take after changing Nginx port in Linux is to update your firewall rules. By setting up a new rule that allows incoming traffic only on the new port, you can greatly reduce the risk of unauthorized access to your server. This can be done using command-line tools such as UFW or iptables, depending on your Linux distribution.

For example, if you are using UFW, you could run the following command to allow incoming traffic on a specific port −

sudo ufw allow /tcp  

Updating DNS Records if Necessary

If your website or application relies on domain names rather than IP addresses, it is important to update any DNS records that point to your server's old IP address and port. This will ensure that users can still access your site or app without any issues after the change.

Depending on your setup, this may involve updating A records, CNAME records, or other types of DNS entries. Be sure to check with your domain registrar or DNS hosting provider for specific instructions on how to make these updates.

Avoid Common Pitfalls

While changing Nginx port in Linux is generally a straightforward process, there are some common pitfalls that you should try to avoid. For example, it's important not to accidentally leave any references to the old port in your configuration file after making the change - this could cause unexpected behavior and potentially compromise server security.

Conclusion

Changing the Nginx port in Linux is a critical task for server security. In this article, we have discussed how to check the current Nginx configuration and change the port to a more secure one. By following the step-by-step guide and taking additional measures like using firewall rules to restrict access and updating DNS records, you can greatly enhance server security.

Updated on: 24-Aug-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements