美文网首页
在Nginx上配置多个站点的方法

在Nginx上配置多个站点的方法

作者: 努力与幸运 | 来源:发表于2019-04-12 13:09 被阅读0次

    在开发过程中,会需要在nginx中配置多个站点,下面我们来举例说明
    有两个站点:
    站点一:qamanage.magewise.cn 存放目录为:D:\MIIC-MyWork\websit\QA_MANAGE_WEB
    站点二:qa.magewise.cn 存放目录为:D:\MIIC-MyWork\websit\QA_WEB
    在Nginx配置目录下,创建一个”qa_web”目录。本例假设Nginx是默认安装,配置目录在”D:\nginx-1.15.9”
    那么qa_web目录就是:D:\nginx-1.15.9\qa_web
    然后在该目录下创建:qa_web.conf文件
    内容为:

    
     
    
        server {
            listen       80;
            server_name  qa.megawise.cn;
            root D:\MIIC-MyWork\websit\QA_WEB;
            charset utf-8;
            #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;
            }
            location /service/answer/ {
                proxy_pass   http://127.0.0.1:8081/answer/; 
    
            }
            location /service/question/ { 
                proxy_pass   http://127.0.0.1:8082/question/; 
            }
            location /service/user/ {  
                proxy_pass   http://127.0.0.1:8084/user/; 
            }
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #} 
        }
     
    

    在qa_web目录下创建qamanage_web.conf文件,其中内容为:
    server {
    listen 80;
    server_name qamanage.megawise.cn;
    root D:\MIIC-MyWork\websit\QA_MANAGE_WEB;
    charset utf-8;
    #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;
        }
        location /service/answer/ {
            proxy_pass   http://127.0.0.1:8081/answer/; 
    
        }
        location /service/question/ { 
            proxy_pass   http://127.0.0.1:8082/question/; 
        }
        location /service/user/ {  
            proxy_pass   http://127.0.0.1:8084/user/; 
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
    
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #} 
    }
    

    这两个conf文件中只有
    server_name 和 root配置不同
    打开D:\nginx-1.15.9\conf目录下的nginx.conf文件,将虚拟目录的配置文件加入到”http {}”部分的末尾:

    http {
      ...
      include D:/nginx-1.15.9/qa_web/*.conf;
    }
    

    重启Nginx服务
    nginx -s reload

    然后就可以访问两个站点了~~

    最后!一定要记住:
    将两个站点的IP写入host(C:\Windows\System32\drivers\etc)文件

    127.0.0.1 qamanage.megawise.cn
    127.0.0.1 qa.megawise.cn
    

    相关文章

      网友评论

          本文标题:在Nginx上配置多个站点的方法

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