2

Nginx配置负载实例

 1 year ago
source link: https://syxdevcode.github.io/2022/09/20/Nginx%E9%85%8D%E7%BD%AE%E8%B4%9F%E8%BD%BD%E5%AE%9E%E4%BE%8B/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nginx配置负载实例

2022-09-202022-09-30Nginx

1.5k 1 分钟

注意:配置upstream负载 与 单节点 配置有区别,实测发现配置upstream负载的,在location 中 配置 rewrite 不生效,而且需要添加匹配参数。

配置 upstream 的 location 代码:注意添加了 ~*

#代理后端接口
location ~* /api/ {
proxy_pass http://apicluster; #转发请求的地址
rewrite ^/api/(.*)$ /$1 break;
include /etc/nginx/nginxconfig.io/proxy.conf;
}

单节点 location 代码:

#代理后端接口
location /api/ {
proxy_pass https://10.10.0.106:8082; #转发请求的地址
rewrite ^/api/(.*)$ /$1 break;
include /etc/nginx/nginxconfig.io/proxy.conf;
}
upstream apicluster{
server 10.10.0.37:30008;
server 10.10.0.39:30008;
server 10.10.0.183:30008;
server 10.10.0.184:30008;
}

server {
listen 443;
# listen [::]:4433 http2;
server_name 10.10.0.106;

# SSL
# ssl_certificate /etc/nginx/ssl/mysite.com.crt;
# ssl_certificate_key /etc/nginx/ssl/mysite.com.key;

# security
include /etc/nginx/nginxconfig.io/security.conf;

# logging
access_log /var/log/nginx/mysite.access.log;
error_log /var/log/nginx/mysite.error.log warn;

#代理后端接口
location ~* /api/ {
proxy_pass http://apicluster; #转发请求的地址
include /etc/nginx/nginxconfig.io/proxy.conf;
}
# WebSocket
location ~* /hubs/ {
proxy_pass http://apicluster; #转发请求的地址
include /etc/nginx/nginxconfig.io/proxy.conf;
}

location ~* / {
proxy_pass http://10.10.0.106:8080;
include /etc/nginx/nginxconfig.io/proxy.conf;
}

location @router {
rewrite ^.*$ /index.html last;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# additional config
include /etc/nginx/nginxconfig.io/general.conf;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK