1. 程式人生 > 實用技巧 >nginx反向代理部署springboot專案報404無法載入靜態資源

nginx反向代理部署springboot專案報404無法載入靜態資源

問題:nginx反向代理部署springboot專案報404無法載入靜態資源(css,js,jpg,png...)

為什麼要用反向代理:springboot預設啟動埠為8080,如果需要通過域名(不加埠號)直接訪問springboot服務就需要nginx配置反向代理到8080埠

nginx配置vhost主機的過程就不在這講了,重點解決反向代理靜態資源無法訪問

nginx反向代理配置

server
    {
        listen 80;
        #listen [::]:80;
        server_name wms.shiyayun.cn;

        location /{
                 #配置訪問的專案路徑(注:反向代理配置)
                 proxy_pass http:
//127.0.0.1:8080; }

靜態資源無法載入導致頁面樣式無法顯示

靜態資源無法訪問的原因是靜態資源並沒有做反向代理

vhost配置中新增如下配置

     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            #解決反向代理無法訪問圖片
            proxy_pass http://127.0.0.1:8080;
            proxy_set_header   Host $host;
            proxy_set_header X
-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; expires 30d; } location ~ .*\.(js|css)?$ { #解決反向代理無法訪問js,css proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X
-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; expires 12h; }

伺服器重啟nginx服務:

[root@VM_0_2_centos sbin]# cd /usr/local/nginx/sbin/
[root@VM_0_2_centos sbin]# ./nginx -s reload

靜態資源載入成功頁面樣式顯示正常