nginx配置虚拟主机vhost

需求

项目需要两个域名使用同一个服务器,配置多个项目文件。
所谓虚拟主机,是说通过几个不同的url地址,都能到达nginx环境,只不过针对不同的url,处理的逻辑不同。

nginx支持虚拟主机,但是浏览器等客户端不知道,所以虚拟主机的几个地址,应该是都指向nginx所在的ip地址,虚拟主机功能才能正常。

规划

域名a以及域名b的http默认80端口配置官网;
域名a以及域名b的https默认443端口配置接口;
域名a语句域名b的后台管理系统82以及83端口分别配置后台管理系统,然后dns解析在配置隐形url。

配置


官网的两个配置
server {
                listen  80;     
                server_name     a.com;
                
                location / { 
                        root /www/website-novel;
                        index index.html index.htm;
                }
    
    }

        server {
                listen  80;
                server_name     b.com;
                location / {
                        root /www/website-onlinenovel;
                        index index.html index.htm;
                }

    }

接口https的配置

 server {
        listen       8089;
        listen       443 ssl;
	server_name  a.com:8089;

        charset utf8;

        ssl_certificate     /usr/local/nginx/conf/cert/a.com.pem;  # pem文件的路径
        ssl_certificate_key  /usr/local/nginx/conf/cert/a.com.key; # key文件的路径
        
        # ssl验证相关配置
        ssl_session_timeout  5m;    #缓存有效期
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;    #加密算法
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    #安全链接可选的加密协议
        ssl_prefer_server_ciphers on;   #使用服务器端的首选算法

        access_log /var/log/nginx/novel_access.log;
		error_log  /var/log/nginx/novel_error.log;

        location / {
	    #include uwsgi_params;
	    include /usr/local/nginx/conf/uwsgi_params;
	    uwsgi_pass 127.0.0.1:8888;
	    uwsgi_read_timeout 2;
            index  index.html index.htm;
        }
        location /static {
            alias /www/novel/static;
        }
  
    }
	server {
        listen       8087;
        listen       443 ssl;
        server_name  b.com;

    # 域名,多个以空格分开
   ssl_certificate     /usr/local/nginx/conf/cert/b.com.pem;  # pem文件的路径
        ssl_certificate_key  /usr/local/nginx/conf/cert/b.com.key; # key文件的路径

        # ssl验证相关配置
        ssl_session_timeout  5m;    #缓存有效期
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;    #加密算法
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    #安全链接可选的加密协议
        ssl_prefer_server_ciphers on;   #使用服务器端的首选算法
 
        charset utf8;

        access_log /var/log/nginx/novel_access.log;
		error_log  /var/log/nginx/novel_error.log;

        location / {
	    #include uwsgi_params;
	    include /usr/local/nginx/conf/uwsgi_params;
	    uwsgi_pass 127.0.0.1:8889;
	    uwsgi_read_timeout 2;
            index  index.html index.htm;
        }
		location /static {
			alias /www/online_novel/static;
		}
    }

后台管理系统配置
 server {
	    listen	82;
	    server_name	novel.a.com;
		
		location / {
			root /www/inovel/;
			index index.html index.html;		
		}
		location /admin-api {
                    proxy_pass http://127.0.0.1:8080; 
		}
    }
	server {
	    listen	83;
	    server_name	novel.b.com;
		
		location / {
			root /www/online_inovel/;
			index index.html index.html;		
		}
		location /admin-api {
                    proxy_pass http://127.0.0.1:8083; 
		}
    }
最后在两个域名的dns解析分别都配置@解析到该服务器ip即可
最后效果
a官网:
http://a.com/
a接口:
https://a.com/
a后台管理系统:
http://novel.a.com/
b官网:
http://b.com/
b接口:
https://b.com/
b后台管理系统:
http://novel.b.com/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值