SpringBoot整合Nginx實(shí)現(xiàn)反向代理、負(fù)載均衡與HTTPS配置
生產(chǎn)環(huán)境中,SpringBoot 一般不直接暴露端口給用戶,前面都會加一層 Nginx。Nginx 負(fù)責(zé)反向代理、負(fù)載均衡、SSL 證書、靜態(tài)資源緩存等。
一、反向代理
基本配置:
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:9090;
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_set_header X-Forwarded-Proto $scheme;
}
}二、負(fù)載均衡
upstream seckill_backend {
# weight 越大,分配到的請求越多
server 192.168.1.101:9090 weight=3;
server 192.168.1.102:9090 weight=2;
server 192.168.1.103:9090 weight=1;
}
server {
listen 80;
location / {
proxy_pass http://seckill_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}三、HTTPS
sudo apt install certbot python3-certbot-nginx -y sudo certbot --nginx -d api.example.com
server {
listen 443 ssl;
server_name api.example.com;
ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:9090;
}
}四、WebSocket 支持
location /ws {
proxy_pass http://127.0.0.1:9090;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_read_timeout 300s;
}五、靜態(tài)資源緩存
location /static/ {
root /opt/app/;
expires 7d;
add_header Cache-Control "public, immutable";
}六、安全與限流
# 限制同一 IP 每秒最多 10 個(gè)請求
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;
server {
location /api/ {
limit_req zone=api_limit burst=20 nodelay;
proxy_pass http://127.0.0.1:9090;
}
}到此這篇關(guān)于SpringBoot整合Nginx實(shí)現(xiàn)反向代理、負(fù)載均衡與HTTPS配置的文章就介紹到這了,更多相關(guān)SpringBoot整合Nginx內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用ProGuard混淆JavaWeb項(xiàng)目代碼的操作步驟
在開發(fā)JavaWeb應(yīng)用時(shí),為了保護(hù)源代碼不被輕易反編譯和閱讀,通常會采用代碼混淆技術(shù),ProGuard是一個(gè)廣泛使用的免費(fèi)工具,可以用來優(yōu)化、縮小和混淆Java字節(jié)碼,本文將詳細(xì)介紹如何使用ProGuard對JavaWeb項(xiàng)目進(jìn)行代碼混淆,需要的朋友可以參考下2025-05-05
Spring?Boot?+?Spring?Batch?實(shí)現(xiàn)批處理任務(wù)的詳細(xì)教程
這篇文章主要介紹了Spring?Boot+Spring?Batch實(shí)現(xiàn)批處理任務(wù),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
通過實(shí)例解析Spring Ioc項(xiàng)目實(shí)現(xiàn)過程
這篇文章主要介紹了Spring Ioc項(xiàng)目實(shí)踐過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
SpringBoot注冊FilterRegistrationBean相關(guān)情況講解
這篇文章主要介紹了SpringBoot注冊FilterRegistrationBean相關(guān)情況,借助FilterRegistrationBean來注冊filter,可以避免在web.xml種配置filter這種原始的寫法2023-02-02
Spring Cloud入門教程之Zuul實(shí)現(xiàn)API網(wǎng)關(guān)與請求過濾
這篇文章主要給大家介紹了關(guān)于Spring Cloud入門教程之Zuul實(shí)現(xiàn)API網(wǎng)關(guān)與請求過濾的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-05-05
Java 中的 DataInputStream 介紹_動力節(jié)點(diǎn)Java學(xué)院整理
DataInputStream 是數(shù)據(jù)輸入流。它繼承于FilterInputStream。接下來通過本文給大家介紹Java 中的 DataInputStream的相關(guān)知識,需要的朋友參考下吧2017-05-05

