Nginx-多服务绑定80端口及映射域名
多服务绑定80端口及映射域名
说明:业务需要配置的样例模板,如需深入了解,请查看官方文档
1.Nginx配置文件nginx.conf(可拆分多台机器部署)
worker_processes 1;
events {
worker_connections 1024;
}http {
include mime.types;
default_type application/octet-stream;sendfile on;
#tcp_nopush on;#keepalive_timeout 0;
keepalive_timeout 65;#gzip on;
upstream apiServices{
server 192.168.1.100:80;
server 192.168.1.101:80;
server 192.168.1.102:80;
}server {
listen 80;
server_name www.test.com;
client_max_body_size 100M;
location / {
try_files $uri @www;
}
location @www{
internal;
proxy_pass http://127.0.0.1:6666;
include req_proxy.conf;
}
}
server {
listen 80;
server_name maven.test.com;
client_max_body_size 20M;
location / {
try_files $uri @maven;
}
location @maven {
internal;
proxy_pass http://192.168.1.103:8081;
include req_proxy.conf;
}
}server {
listen 80;
server_name pbs.test.com;
client_max_body_size 20M;
location / {
try_files $uri @pbs;
}
location @pbs {
internal;
proxy_pass http://192.168.1.103:8888;
include req_proxy.conf;
}
}server {# www.test.com绑定网址
listen 6666;
server_name localhost;
charset utf-8;
server_name_in_redirect off;
port_in_redirect off;gzip on;
gzip_static on;
gzip_min_length 10k;
gzip_types text/javascript application/javascript text/css application/json;
gzip_proxied any;
gzip_vary on;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
root D:/web/dist;#vue项目
index index.html index.htm; #目录内的默认打开文件,如果没有匹配到index.html,则搜索index.htm,依次类推--root的详解版本
location /{
try_files $uri $uri//index.html;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
}location = /index.html {
add_header Cache-Control no-cache;
}location ~ /static/ {
add_header Cache-Control no-cache;
}location ~ /(js/*|css/*|img/*|font/*) {
expires 30d;
add_header Cache-Control public;
}
location /api/{#要访问的后端服务
proxy_pass http://apiServices;
proxy_cookie_path '/api' '';
#sub_filter '127.0.0.1:8181' '192.168.1.103/api/';
#sub_filter_once off;
}
}server { #pbs.test.com绑定资源
listen 8888;
server_name localhost;
charset utf-8;
server_name_in_redirect off;
port_in_redirect off;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /photos {
root D:/resources;
rewrite ^/photos/(.*)$ \photos\$1 break;
}
location /videos {
root D:/resources;
rewrite ^/videos/(.*)$ \videos\$1 break;
}}
}
2.include引入统计配置req_proxy.conf
proxy_connect_timeout 30s;
proxy_send_timeout 120;
proxy_read_timeout 120;
proxy_buffer_size 32k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_redirect off;
proxy_hide_header Vary;
proxy_set_header Accept-Encoding '';
proxy_set_header Host $host;
proxy_set_header Referer $http_referer;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;