nginx 代理 springboot 項目

注:centos7 服務器一臺,可用域名一個,springboot web應用一套

下面來描述一下nginx+springboot+centos7 如何部署吧:

1.應用編寫(本項目使用springboot編寫)注意項目的application.yml(property配置為server.context-path=/yyy/)的配置文件添加如下配置


server: port: xxx context-path: /yyy/

上面的 xxx 為項目端口 ,yyy 為項目名稱或者為項目起一個特定的標識(一串字符就可以,但必須以斜槓開始,否則會報錯,小編已近採坑)

2.安裝 nginx 參考 https://blog.csdn.net/oldguncm/article/details/78855000 當然也可以參考其他博客,網上有很多的

3.有一個可用的域名(沒有就去買一個,很便宜的)

4.配置nginx

修改配置文件 /etc/nginx/nginx.conf(此處的yyy和第一步的yyy必須對應,不然靜態資源會因為路徑錯誤出現404)


server { listen 80 default_server; server_name my.com; # Load configuration files for the default server block. # include /etc/nginx/default.d/*.conf; location /yyy/ { # 項目一 proxy_pass http://104.12.1.3:xxx/yyy/; # 項目1對應的ip和端口 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 404 /404.html; location = /40x.html { }}

5.重啟nginx

systemctl restartstart nginx 也可以 service nginx restart

6.啟動你的springboot 項目

照例附上美圖!更多精彩內容請關注百戰程序員!另外,求關注吶求收藏吶!

7.訪問 http:www.my.com/yyy/login 此處的login為你的項目訪問入口,也可以為其他的路徑