程序员

Nginx配置不同端口虚拟主机

2018-11-29  本文已影响3人  那年六月六

Nginx简易配置不同端口的虚拟主机

系统环境及nginx版本:

[root@localhost nginx-1.15.5]# uname -r && cat /etc/redhat-release 
3.10.0-693.el7.x86_64
CentOS Linux release 7.4.1708 (Core) 
[root@localhost nginx-1.15.5]# nginx -v
nginx version: nginx/1.15.5

修改配置文件,添加如下内容:

 [root@localhost nginx-1.15.5]# vim /usr/local/nginx/conf/nginx.confv
···
    server {
        listen       8080;
        server_name  localhost;
        root /usr/share/nginx/html;
    }
    server {
        listen       8081;
        server_name  localhost;
        root /usr/share/nginx/html1;
    }
    server {
        listen       8082;
        server_name  localhost;
        root /usr/share/nginx/html2;
    }
···

创建各自nginx根目录下访问的文件:

[root@localhost nginx-1.15.5]# ll  /usr/share/nginx/html*/index.html 
-rw-r--r-- 1 root root 613 Nov 29 17:06 /usr/share/nginx/html1/index.html
-rw-r--r-- 1 root root 614 Nov 29 17:06 /usr/share/nginx/html2/index.html
-rw-r--r-- 1 root root 610 Nov 29 16:36 /usr/share/nginx/html/index.html

重启nginx:

[root@localhost nginx-1.15.5]# nginx  -s stop && nginx

浏览器分别打开:
http://192.168.1.117:8080/
http://192.168.1.117:8081/
http://192.168.1.117:8082/

END

上一篇 下一篇

猜你喜欢

热点阅读