一般在模塊項目中,每個模塊幾乎是獨立的,這樣也就有多個spring-boot項目,端口不一樣,但是對外的端口我們只能是80端口,這種情況下我們就要使用nginx來做反向代理,來部署多個spring-boot項目。
下面是nginx.conf中的基本的配置:
server {
listen 80;
server_name 127.0.0.1;
access_log logs/book.log;
error_log logs/book.error;
#將/wxService請求轉發給http://127.0.0.1:8080/wxService處理
location /wxService {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080/wxService;
}
#將/ddService請求轉發給http://127.0.0.1:8081/ddService處理
location /ddService{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8081/ddService;
}
#將/eurekaService請求轉發給http://127.0.0.1:8082/eurekaService處理
location /eurekaService {
proxy_pass http://127.0.0.1:8082/eureka-service; #這裏的端口記得改成項目對應的哦
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
#將/api請求轉發給http://127.0.0.1:8083/api處理
location /api {
proxy_pass http://127.0.0.1:8083/api; #這裏的端口記得改成項目對應的哦
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
.... ....
# 後續在添加新增的項目即可
}
注意,這裏的項目必須提前在springBoot的application.yml文件中設置:
server:
context-path : /ddService
修改好nginx.conf以後我們就需要重啓nginx了
ps -ef | grep nginx
kill -9 IP
start nginx
然後刷新瀏覽器,測試幾個項目飾釦可以正常訪問,最後一定注意在項目中設置context-path的項目路徑,不然你會奔潰。。。。