两种方法:
1: 在nginx.conf中配置location中的alisa值为本地
server{
listen 80;
server_name www.xxxxx.com;
ssi on;
ssi_silent_errors on;
location / {
alias F:/teach/xcEdu/xcEduUI/xc-ui-pc-static-portal/;
index index.html;
}
}
F:/teach/xcEdu/xcEduUI/xc-ui-pc-static-portal/ 本目录即为门户的主目录。
如果出现:Invalid Host header 就是hosts文件没有配置。
配置hosts文件
本教程的开发环境使用Windows 7,修改C:\Windows\System32\drivers\etc\hosts文件
127.0.0.1 www.xxxxx.com
2: 1、模拟n个http服务器作为目标主机
用作测试,简单的使用2个tomcat实例模拟两台http服务器,分别将tomcat的端口改为8081和8082
2、配置IP域名
192.168.72.49 8081.max.com
192.168.72.49 8082.max.com
3、配置nginx.conf
#upstream表示负载服务器池,定义名字为tomcatserver1 的服务器池
upstream tomcatserver1 {
server 192.168.72.49:8081;
}
upstream tomcatserver2 {
server 192.168.72.49:8082;
}
server {
listen 80;
server_name 8081.max.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver1;
index index.html index.htm;
#下面这两条配置,意思是将http头转发给后端应用,不然你后端应用服务拿客户端IP地址的时候拿到的是nginx代理的地址而不是客户端的。
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
listen 80;
server_name 8082.max.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatserver2;
index index.html index.htm;
}
}
做完之后还得去配置hosts文件。
如果是vue项目,要在项目目录的config目录下的index.js文件中,配置host:‘虚拟域名’,再启动vue项目就会按照这个名字启动。
流程:
1)浏览器访问8081.max.com,通过本地host文件域名解析,找到192.168.72.49服务器(安装nginx)
2)nginx反向代理接受客户机请求,找到server_name为8081.max.com的server节点,根据proxy_pass对应的http路径,将请求转发到upstream tomcatserver1上,即端口号为8081的tomcat服务器。
原文地址:https://www.cnblogs.com/jmao/p/9908559.html