1、替换springboot网关
server {

listen 80;

server_name abc.cn;

location /abc {
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
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 以/结尾
location /defg {
proxy_pass http://127.0.0.1:8081/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
说明1:
web访问:abc.cn/abc/a.html
对应服务器: 127.0.0.1:8080/abc/a.html
说明2:
web访问: abc.cn/defg/b.html
对应服务器: 127.0.0.1:8081/b.html
说明3: 区别在于proxy_pass末尾有没有斜杠
说明4: 可任意修改代理的服务器以及端口
2、vue配置
1)完整server
server {
listen 13694;
server_name localhost 192.168.100.100;
# gzip config
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";

location / {
root /var/lib/jenkins/workspace/hg-ui/dist;
index index.html;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_send_timeout 30m;
proxy_read_timeout 30m;
client_max_body_size 100m;
try_files $uri $uri/ /index.html;
}
}

3、代理websocket
1)完整server
server
{
listen 80;
server_name api.zzhxbd.cn;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/api.zzhxbd.cn;

#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#SSL-END

#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END

#PHP-INFO-START PHP引用配置,可以注释或修改
include enable-php-00.conf;
#PHP-INFO-END

#REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
include /www/server/panel/vhost/rewrite/api.zzhxbd.cn.conf;
#REWRITE-END

#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}

#一键申请SSL证书验证目录相关设置
location ~ \.well-known{
allow all;
}

#禁止在证书验证目录放入敏感文件
if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) {
return 403;
}

access_log /www/wwwlogs/api.zzhxbd.cn.log;
error_log /www/wwwlogs/api.zzhxbd.cn.error.log;

location / {
proxy_pass http://127.0.0.1:26666;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 支持websocket
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
2)单独location
location / {
proxy_pass http://127.0.0.1:26666;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 支持websocket
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
~~
4、代理tcp
1)域名代理tcp
2)依赖stream模块
3)配置
stream {
map $ssl_preread_server_name $name {
mysql.test.com mysql;
redis.test.com redis;
default https_default_backend;
}
upstream mysql{
server 10.0.0.3:3306;
}
upstream redis{
server 10.0.0.4:6379;
}
upstream https_default_backend {
server 127.0.0.1:443;
}
server {
listen 80;
proxy_pass $name;
ssl_preread on;
}
}
~~