Nginx基于端口虚拟主机配置



在/etc/nginx/conf.d目录下创建

文件default.conf

server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

}

文件default2.conf
server {
listen 81;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

}

检查nginx配置的语法是否正确

nginx -tc /etc/nginx/nginx.conf

关闭nginx服务

nginx -s stop -c /etc/nginx/nginx.conf

启动nginx服务

nginx -c /etc/nginx/nginx.conf

测试:

http://192.168.237.128:80

http://192.168.237.128:81

test2026-06-10 22:24