nginx 多域名
需求: nginx 使用单IP多域名转发需要 使用多个配置!
网站A :127.0.0.1:8080 A.COM
网站B:127.0.0.1:8092 B.COM
首先配置主要配置文件 nginx.conf
#user nobody;
#工作的子进程数量(通常等于CPU数量或者2倍于CPU)
worker_processes 1;
#错误日志存放路径
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#指定pid存放文件
pid logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
#使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
#use epoll;
#允许最大连接数
worker_connections 1200;
}
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 D:/hwy/nginx-1.8.0/logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 120;
upstream aTestServer {
server 127.0.0.1:8080;
}
include A.conf;
upstream bTestServer {
server 127.0.0.1:8092;
}
include B.conf;
}
A.CONF配置
server{
listen 80;
server_name A.com;
index index.html index.htm;
root D:\tomcat-mobile\webapps;
#配置首页精确匹配
location = / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://bTestServer;
}
#配置首页
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://bTestServer;
}
#动态页面交给http://127.0.0.1:8080,也即我们之前在nginx.conf定义的upstream bTestServer 均衡
location ~ .*\.(php|jsp|cgi|js|css|gif|jpg|jpeg|png|bmp|swf|shtml|htm|html|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://bTestServer;
}
#配置静态资源前缀匹配
location ~ ^/(fileUpload|doc)/ {
root D:\tomcat-mobile;
access_log off;
expires 4d;
}
#配置Nginx动静分离,定义的静态页面直接从项目指定目录读取。
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|ico)$ {
access_log off;
expires 30d;
}
#定义Nginx输出日志的路径
#access_log D:\tomcat-mobile\logs\access.log main;
#error_log D:\tomcat-mobile\logs\error.log crit;
}
B.CONF 设置
server{
listen 80;
server_name B.COM;
index index.html index.htm;
root D:\tomcat\webapps;
#配置首页精确匹配
location = / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://aTestServer;
}
#配置首页
location / {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://aTestServer;
}
#动态页面交给http://127.0.0.1:8092,也即我们之前在nginx.conf定义的upstream aTestServer 均衡
location ~ .*\.(php|jsp|cgi|js|css|gif|jpg|jpeg|png|bmp|swf|shtml|htm|html|ico|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma|ioc)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://aTestServer;
}
#配置静态资源前缀匹配
location ~ ^/(fileUpload|doc)/ {
root C:\Users\Administrator\Desktop\resin-4.0.52\webapps;
access_log off;
expires 4d;
}
#location ~ ^/favicon\.ico/{
# root C:\Users\Administrator\Desktop\nginx-1.11.10;
#}
#配置Nginx动静分离,定义的静态页面直接从项目指定目录读取。
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
access_log off;
expires 30d;
}
#定义Nginx输出日志的路径
#access_log D:\tomcat\logs\access.log main;
#error_log D:\tomcat\logs\error.log crit;
}
浙公网安备 33010602011771号