美文网首页程序员IT@程序员猿媛程序员
Nginx通过端口号区别不用的虚拟主机

Nginx通过端口号区别不用的虚拟主机

作者: 您好简书 | 来源:发表于2019-08-06 22:57 被阅读6次

    第一步:

     cd /usr/local/nginx
    

    第二步:找到conf文件 并且进入
    输入

    ll
    
    
    [root@Nigux nginx]# ll
    总用量 4
    drwxr-xr-x. 2 root root 4096 7月  28 01:39 conf
    drwxr-xr-x. 2 root root   40 7月  28 01:39 html
    drwxr-xr-x. 2 root root   19 7月  28 01:39 sbin
    [root@Nigux nginx]# cd conf
    [root@Nigux conf]# ll
    总用量 60
    -rw-r--r--. 1 root root 1034 7月  28 01:39 fastcgi.conf
    -rw-r--r--. 1 root root 1034 7月  28 01:39 fastcgi.conf.default
    -rw-r--r--. 1 root root  964 7月  28 01:39 fastcgi_params
    -rw-r--r--. 1 root root  964 7月  28 01:39 fastcgi_params.default
    -rw-r--r--. 1 root root 2837 7月  28 01:39 koi-utf
    -rw-r--r--. 1 root root 2223 7月  28 01:39 koi-win
    -rw-r--r--. 1 root root 3957 7月  28 01:39 mime.types
    -rw-r--r--. 1 root root 3957 7月  28 01:39 mime.types.default
    -rw-r--r--. 1 root root 2656 7月  28 01:39 nginx.conf
    -rw-r--r--. 1 root root 2656 7月  28 01:39 nginx.conf.default
    -rw-r--r--. 1 root root  596 7月  28 01:39 scgi_params
    -rw-r--r--. 1 root root  596 7月  28 01:39 scgi_params.default
    -rw-r--r--. 1 root root  623 7月  28 01:39 uwsgi_params
    -rw-r--r--. 1 root root  623 7月  28 01:39 uwsgi_params.default
    -rw-r--r--. 1 root root 3610 7月  28 01:39 win-utf
    

    第三步:进入nginx.conf 配置文件

    vim nginx.conf
    

    找到这个节点

    server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
    

    解释一下

    server 中 listen 80; 代表监控的80端口

    root html; 代表 安装程序所在的目录

    因为我们把Nginx安装在cd /usr/local/nginx/

    image.png

    目录下的html 就是上面所指的本地的html

    index index.html index.htm; 表示欢迎页

    image.png
    也就是说 一个server 就是一个虚拟主机

    通过配置不同的端口,实现不同的网站

    接下来,我们要修改配置文件 下载一个好用的文本编辑器EditPlus

    image.png image.png

    然后在左边下拉框 选择

    image.png

    完全实现在windows下修改 server

    删除一些带#号注释的部分 保存后 直接就能保存到服务端

    接下来 我们只需要复制 server 修改其中的端口号就可以了

    复制 一份 注意括号

    http {
        include       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  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
        }
    
        server {
            listen       81;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html81;
                index  index.html index.htm;
            }
    }
    }
    

    为了区分 之前端口,和欢迎界面 我们改为html81 但是跟目录下并没有html81

    所以 在执行 本文中 第一步 cd /usr/local/nginx

    如果复制目录 我们就加 -r 如果要是复制文件 就直接cp
    复制cp -r html html81

    image.png

    修改一下 这个html81 区分

    命令

    vim html81/index.html
    

    进入编辑

    
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    <p>For online documentation and support please refer to
    <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    
    
    

    修改

    image.png

    修改完之后

    我们要重新加载一下

    [root@Nigux nginx]# sbin/nginx -s reload
    

    测试 http://192.168.191.129/:80

    image.png

    测试http://192.168.191.129:81/

    image.png

    相关文章

      网友评论

        本文标题:Nginx通过端口号区别不用的虚拟主机

        本文链接:https://www.haomeiwen.com/subject/ckwgdctx.html