The impact of appending slashes to nginx secondary directories
为情所困
为情所困 2017-05-16 17:29:56
0
1
664

My web program is in the app1 directory. When accessed using http://server.com/app1/, everything is normal. However, when accessed using http://server.com/app1, the hyperlinks in the page will be Miss this secondary directory, such as

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

points to http://server.com/static/sytle.css instead of http://server.com/app1/static/sytle.c... causing resource acquisition failure.

I know I can jump by adding rules

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

But this will add an extra slash to the URL. Is there any other way?

The following is my nginx configuration:

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;
        }
为情所困
为情所困

reply all(1)
阿神

As DaiJie said in the comments, it is very normal for this problem to occur, and the default behavior of the web server and browser is correct.
It is recommended that the subject use absolute paths or complete addresses including domain names to reference static resources.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template