nginx 二級目錄附加斜線的影響
为情所困
为情所困 2017-05-16 17:29:56
0
1
595

我的網頁程式在app1 目錄下, 當用http://server.com/app1/ 訪問時,一切正常,但是用http://server.com/app1 訪問時,頁面裡的超連結會漏掉這個二級目錄,例如

<link type="text/css" rel="stylesheet" href="static/style.css" />

指向的是 http://server.com/static/sytle.css 而不是 http://server.com/app1/static/sytle.c... 導致資源取得失敗。

我知道可以透過新增規則來跳到

rewrite ^([^.]*[^/])$ / permanent;

但是這樣就讓url上多出了一個斜槓,有沒有其他辦法?

以下是我的nginx配置:

set $dir /home/user/apps;
        location ~ static/(.*)$ {
            alias $dir/$uri;
        }
        location ~ ^/([^/]+) {
            set $app ;
            include uwsgi_params;
            uwsgi_pass unix:///tmp/uwsgi_vhosts.sock;
            uwsgi_param UWSGI_CHDIR $dir/$app/;
            uwsgi_param UWSGI_PYHOME $dir/$app/;
            uwsgi_param PATH_INFO /;
            uwsgi_param UWSGI_SCRIPT index;
            uwsgi_param SERVER_NAME $server_name.$app;
        }
为情所困
为情所困

全部回覆(1)
阿神

正如評論裡 DaiJie 所說的,出現這個問題的非常正常,web server 和瀏覽器預設行為是正確的。
建議題主使用絕對路徑或含網域的完整位址來引用靜態資源。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!