解决nginx反向代理后页面上的js/css文件无法加载

问题现象:

nginx配置反向代理后,网页可以正常访问,但是页面上的js、css和图片等资源都无法访问。

(1)nginx配置如下:
(2)域名访问:js css文件无法加载;
(3)IP访问:js css文件可以正常加载;
(4)CI框架下无法访问
配置此例即可:


location / {
                proxy_pass http://127.0.0.1:8000;
                include naproxy.conf;
        }

解决方法:

nginx配置文件中,修改为如下配置:

 location ~ \.php$ {
                proxy_pass http://127.0.0.1:8000;
                include naproxy.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
                expires      30d;
                proxy_pass http://127.0.0.1:8000;
                include naproxy.conf;
        }

        location ~ .*\.(js|css)?$ {
                expires      12h;
                proxy_pass http://127.0.0.1:8000;
                include naproxy.conf;
        }

需要把静态文件也添加反向代理设置。

原因分析:

反向代理的路径下找不到文件,这里需要单独指定js和css文件的访问路径。