search

Home  >  Q&A  >  body text

The impact of appending slashes to nginx secondary directories

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

reply all(1)I'll reply

  • 阿神

    阿神2017-05-16 17:31:56

    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.

    reply
    0
  • Cancelreply