How to Change the Default Port Number of Nginx on CentOS 7.6

Changing the default port number of Nginx on CentOS 7.6

Check the version of default port number of nginx using the following command.

[root@linuxhelp ~]# netstat -tulpn | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      32430/nginx: master 

Modify the configuration file of Nginx server to change the default port number

[root@linuxhelp ~]# vim /etc/nginx/nginx.conf 
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server {
	listen       8989 default_server;
	listen       [::]:8989 default_server;
	server_name   _;
	root         /usr/share/nginx/html;
}
}

Test the configuration file of Nginx server.

[root@linuxhelp ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart the service of Nginx server.

[root@linuxhelp ~]# systemctl restart nginx.service 

Search for the listening socket 8989 using netstat.

[root@linuxhelp ~]# netstat -tulpn | grep 8989
tcp        0      0 0.0.0.0:8989            0.0.0.0:*               LISTEN      2260/nginx: master  
tcp6       0      0 :::8989                 :::*                    LISTEN      2260/nginx: master  

The default port number of Nginx (80) has changed to 8989 successfully and this tutorial comes to an end with this.

Tag : Nginx CentOS
Comment
angga92
Jun 18 2020
I have followed step by step above,
but I stopped stuck on the system restart restart nginx
notif error : Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
after I edit / add in nano /etc/nginx/nginx.conf
beg for your help and enlightenment

thank you
Add a comment
FAQ
Q
What are nginx server blocks ?
A
Server block in nginx server ables to host multiple websites on a single server
Q
What is a reverse proxy cache in nginx server?
A
a reverse proxy in nginx server is a type of proxy server that typically sits behind the firewall in a private network and directs the clients requests to the appropriate backend server
Q
What is the use of load balancer in nginx server?
A
Load balancer is a device that acts as a reverse proxy and distributes the network or traffic across a number of servers which increases the capacitty abd reliability of applications that use nginx server.
Q
What is load balancing in nginx server?
A
Load balancing in nginx server distributes incoming traffic across a group of backend servers.
Q
What is the default port number of nginx server ?
A
the default port number of nginx server is 80