Home > Blockchain >  Rewriting nginx url isn't working as expected
Rewriting nginx url isn't working as expected

Time:11-25

I've modified my code to this to try to redirect pictures folder to /pictures:

server {
  server_name domain.com;

  location /pictures {
    root /root/some-folder/pictures/;
  }

  listen 443 ssl;
  ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
  include /etc/letsencrypt/options-ssl-nginx.conf;
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

server {
  if ($host = domain.com) {
      return 301 https://$host$request_uri;
  }

  server_name domain.com;
  listen 80;
  return 404;
}

But it's just not working. What's the reason?

CodePudding user response:

Some explanation for my comment:

Nginx behavior for:

root:

location /pictures {
    root /root/some-folder/pictures/;
}

This translates to:

/root/some-folder/pictures/pictures;

The root Keyword sets the new fileroot and appends the location path.


alias:

location /pictures {
    alias /root/some-folder/pictures/;
}

This translates to:

/root/some-folder/pictures;

The alias Keyword remaps the path.


Some more examples:

location /static {
    alias /root/some-folder/assets/;
}

# (Request)     https://domain.tld/static/img.png
# (Loaded File) /root/some-folder/assets/img.png

# (Request)     https://domain.tld/static/css/style.css
# (Loaded File) /root/some-folder/assets/css/style.css
location /static {
    root /root/some-folder/assets/;
}

# (Request)     https://domain.tld/static/img.png
# (Loaded File) /root/some-folder/assets/static/img.png

# (Request)     https://domain.tld/static/css/style.css
# (Loaded File) /root/some-folder/assets/static/css/style.css
  • Related