nginx的根目录是:D:\Sys\server\nginx\nginx-1.9.4\
nginx配置文件目录:D:\Sys\server\nginx\nginx-1.9.4\conf
nginx.conf 配置文件如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
include vhosts/*.conf;
}
这个主目录只保留基本配置
include vhosts/*.conf;这句话就是引用虚拟主机目录的配置文件
在该目录下面创建vhosts目录:
D:\Sys\server\nginx\nginx-1.9.4\conf\vhosts
虚拟主机(实际里面就是配置upstream和server,然后server里面配置监听端口和serverName,还有location)目录里面的配置文件分别为:
www.abin.com.conf
www.lee.com.conf
www.abin.com.conf的配置为:
upstream abin {
server localhost:9200 weight=10;
}
# another virtual host using mix of IP-, name-, and port-based configuration
server {
listen 8000;
server_name www.abin.com abin.com;
location / {
#反向代理的地址
proxy_pass http://abin;
root html;
index index.html index.htm;
}
location /abin {
#反向代理的地址
proxy_pass http://abin;
root html;
index index.html index.htm;
}
}
www.lee.com.conf的配置为:
upstream lee {
server localhost:9300 weight=10;//这个配置为tomcat的请求地址
}
# another virtual host using mix of IP-, name-, and port-based configuration
server {
listen 8000;
server_name www.lee.com lee.com;
location / {
#反向代理的地址
proxy_pass http://lee;
root html;
index index.html index.htm;
}
location /abin {
#反向代理的地址
proxy_pass http://lee;
root html;
index index.html index.htm;
}
}
测试:
http://www.lee.com:8000/
http://www.lee.com:8000/abin
http://lee.com:8000/abin
http://www.lee.com:8000/
http://www.lee.com:8000/lee
http://lee.com:8000/lee
我的tomcat服务器的web.xml都配置了<webcome-list>index.html</welcome-list>,
tomcat:9200配置了abin这个java工程,它里面的index.html内容为hello,abin
tomcat:9300配置了abin这个java工程,它里面的index.html内容为hello,lee
那么上面的第二个地址打印hello,abin
那么上面的第四个地址打印hello,lee
本地hosts文件配置为:
127.0.0.1 localhost
127.0.0.1 www.abin.com abin.com
127.0.0.1 www.lee.com lee.com