在Ubuntu上修改Nginx的默认端口(例如从80端口改为其他端口,如8080)
备份原始配置文件
wqbboy@mail:~$ sudo netstat -tulnp |grep 80
[sudo] password for wqbboy:
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 884/nginx: master p
tcp 0 0 127.0.0.1:7791 0.0.0.0:* LISTEN 880/uwsgi
tcp 0 0 127.0.0.1:9999 0.0.0.0:* LISTEN 809/php-fpm: master
tcp6 0 0 :::80 :::* LISTEN 884/nginx: master pwqbboy@mail:~$ sudo cp /etc/nginx/sites-available/00-default.conf /etc/nginx/sites-available/00-default.confbak
编辑Nginx配置文件 ,修改监听端口
在打开的配置文件中,找到listen指令,并将其从listen 80;改为你想使用的端口,例如listen 8080;。确保整个文件中所有的listen 80;都被替换为新的端口号。
server {listen 80;...
}
wqbboy@mail:~$ sudo vim /etc/nginx/sites-available/00-default.conf
重新加载Nginx配置
wqbboy@mail:~$ sudo systemctl restart nginx.service
wqbboy@mail:~$ curl http://localhost:8008
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
验证更改
为了确保Nginx正在监听新的端口,你可以使用netstat或ss命令来检查。例如:
wqbboy@mail:~$ sudo netstat -tulnp |grep 80 tcp 0 0 127.0.0.1:7791 0.0.0.0:* LISTEN 880/uwsgi
tcp 0 0 127.0.0.1:9999 0.0.0.0:* LISTEN 809/php-fpm: master
tcp 0 0 0.0.0.0:8008 0.0.0.0:* LISTEN 3517/nginx: master
tcp6 0 0 :::80 :::* LISTEN 3517/nginx: master
wqbboy@mail:~$ sudo vim /etc/nginx/sites-available/00-default.conf
wqbboy@mail:~$ sudo systemctl restart nginx.service
wqbboy@mail:~$ sudo netstat -tulnp |grep 80
tcp 0 0 127.0.0.1:7791 0.0.0.0:* LISTEN 880/uwsgi
tcp 0 0 127.0.0.1:9999 0.0.0.0:* LISTEN 809/php-fpm: master
tcp 0 0 0.0.0.0:8008 0.0.0.0:* LISTEN 3637/nginx: master
tcp6 0 0 :::8008 :::* LISTEN 3637/nginx: master
wqbboy@mail:~$ curl http://localhost:8008
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
wqbboy@mail:~$ curl http://localhost:80
curl: (7) Failed to connect to localhost port 80 after 1 ms: Connection refused
wqbboy@mail:~$