一、Nginx部署多个Web应用(不同IP地址)
#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;
upstream www.yfscms.com {
server 127.0.0.1:8080;
}
upstream www.yfscms.net {
server 127.0.0.1:9999;
}
upstream www.yfscms.cn {
server 127.0.0.1:8888;
}
upstream weixin.yfscms.com {
server 127.0.0.1:8080;
}
upstream www.gyhkyl.com {
server 127.0.0.1:8899;
}
upstream 120.26.234.50 {
server 127.0.0.1:8899;
}
upstream demo.zcgl.yfscms.com {
server 127.0.0.1:7777;
}
server {
listen 80;
server_name www.yfscms.com;
location / {
index index.html index.jsp;
proxy_pass http://www.yfscms.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.yfscms.net;
location / {
index index.html index.jsp;
proxy_pass http://www.yfscms.net;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.yfscms.cn;
location / {
index index.html index.jsp;
proxy_pass http://www.yfscms.cn;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name weixin.yfscms.com;
location / {
index index.html index.jsp;
proxy_pass http://weixin.yfscms.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.gyhkyl.com;
location / {
index index.html index.jsp;
proxy_pass http://www.gyhkyl.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name 120.26.234.50;
location / {
index index.html index.jsp;
proxy_pass http://120.26.234.50;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name demo.zcgl.yfscms.com;
location / {
index index.html index.jsp;
proxy_pass http://demo.zcgl.yfscms.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
规则说明:
1、一个upstream对应一个server
2、server中的”server_name“跟”proxy_pass“跟upstream对应
二、Nginx部署多个Web应用(同一个IP地址)
#运行用户
#user somebody;
#启动进程,通常设置成和cpu的数量相等
worker_processes 1;
#全局错误日志
error_log C:/Users/wangcw/Desktop/nginx-1.13.12/logs/error.log;
error_log C:/Users/wangcw/Desktop/nginx-1.13.12/logs/notice.log notice;
error_log C:/Users/wangcw/Desktop/nginx-1.13.12/logs/info.log info;
#PID文件,记录当前启动的nginx的进程ID
pid C:/Users/wangcw/Desktop/nginx-1.13.12/logs/nginx.pid;
#工作模式及连接数上限
events {
worker_connections 1024; #单个后台worker process进程的最大并发链接数
}
http {
#设定mime类型,类型由mime.type文件定义
include C:/Users/wangcw/Desktop/nginx-1.13.12/conf/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 C:/Users/wangcw/Desktop/nginx-1.13.12/logs/access.log main;
rewrite_log on;
#设置访问的web应用列表
upstream product_server{
server www.aabbccdd.com:18080;
}
upstream admin_server{
server www.aabbccdd.com:28080;
}
upstream finance_server{
server www.aabbccdd.com:38080;
}
#HTTP服务器
server {
#监听80端口,80端口是知名端口号,用于HTTP协议
listen 80;
#定义使用www.xx.com访问
server_name www.aabbccdd.com;
#首页
index index.jsp
#指向webapp的目录
root C:/XMCARES_X/WorkSpace/nginx/src/main/webapp;
#编码格式
charset utf-8;
#代理配置参数
proxy_connect_timeout 180;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_set_header Host $host;
proxy_set_header X-Forwarder-For $remote_addr;
#默认指向product的server
location / {
proxy_pass http://product_server;
}
#使用location对不同请求做相应处理
location /product/{
proxy_pass http://product_server;
}
location /admin/ {
proxy_pass http://admin_server;
}
location /finance/ {
proxy_pass http://finance_server;
}
}
}