4

I'm trying to redirect mywebsite.com/some-directory to a different directory other than the root of mywebsite.com. I used the following configuration :

server {
  server_name mywebsite.com www.mywebsite.com;
  listen 80;

  root /path/to/the/root/directory/of/mywebsite;
  index index.php index.html index.htm get.html ;

  ssl on;
  listen 443 ssl;
  ssl_certificate my_ssl.crt;
  ssl_certificate_key my_key.key;

  location /some-directory {
    root /path/to/directory;
  }
}

The configuration loads the html file but doesn't load the assets in the same directory.

I found a similar question (or probably the exact one), but the issue was different. I was able to fix that on my own.

I also tried using the alias as suggested in the answer above; but in vain.

My /path/to/directory/ has some-directory within it, and nginx serves the index.html file just fine. The problem occurs when the HTML file tries to access the files (or folders) within /path/to/directory/some-directory. They all return 404 Not Found.

What could I be doing wrong?

shine
  • 69
  • 1
  • 2
  • 4
  • What is the URL of these other resource files, and what is their physical location within your document root? – Richard Smith Feb 28 '16 at 18:50
  • @RichardSmith, the physical files are all within `some-directory` itself. Say, the images are in `some-directory/assets/images`, css in `some-directory/assets/css/`, js in `some-directory/assets/js` and others in `some-directory/assets` as well. – shine Feb 29 '16 at 18:37
  • And what URL is used to access these files? `http://mywebsite.com/some-directory/assets/css/filename.css` or something else? – Richard Smith Feb 29 '16 at 19:20
  • Yes. The same. Those assets are 404. – shine Feb 29 '16 at 19:24
  • Does `nginx-error.log` contain a detailed error message for the 404? – Richard Smith Feb 29 '16 at 19:27
  • Nothing that we already don't know. `[error] 6499#0: *xx open() "/path/to/some/directory/assets/images/image.png" failed (2: No such file or directory), client: xxx.xxx.xxx.xxx, server: mywebsite.com, request: "GET /some-directory/assets/images/image.png HTTP/1.1", host: "mywebsite.com", referrer: "https://mywebsite.com/some-directory/"` – shine Feb 29 '16 at 19:33
  • I presume file permissions and ownership all look fine. No symlink or SELinux oddities? I am out of suggestions now. – Richard Smith Feb 29 '16 at 19:38
  • I don't think so. I even tried 777 permissions (though I reverted them). – shine Feb 29 '16 at 19:42
  • "Nginx locations are exclusive." http://stackoverflow.com/questions/23791415/nginx-alias-not-working# Please see my updated answer and try that. – user5870571 Feb 29 '16 at 19:54
  • Is this on topic on SF? – Marco Sep 14 '17 at 19:39

2 Answers2

2

I found a duplicate of this question but it is on StackOverflow not ServerFault.

https://stackoverflow.com/questions/21628056/virtual-directory-counterpart-in-nginx

To redirect mywebsite.com/some-directory to a different directory than the root of your website you would have something like the code below.

root /path/to/the/root/directory/of/mywebsite;
index index.php index.html index.htm get.html;

location /some-directory/ {
    alias /path/to/some-directory/;
        index index.php;

        try_files $uri $uri/ /index.php?$args;
}

Also read this thread about a similar problem.

user5870571
  • 3,094
  • 2
  • 12
  • 35
0

Searched the web extensively for a solution to this but couldn't find one. Came up with a solution that works.

Consider the case where your website's root is set to /data/mywebsite, so a request to www.mywebsite.com/a/b.html gets /data/mywebsite/a/b.html in response. Say we want all requests starting with www.mywebsite.com/blog to go to a different directory, namely /home/otherfolder. So a request such as www.mywebsite.com/blog/first.html gets /home/otherfolder/first.html in response.

In the above case, you would create a soft symlink in the /data/mywebsite directory that points to /home/otherfolder. So to nginx it would appear as if all the files of otherfolder are present inside /data/mywebsite/blog.

cd /data/mywebsite
ln -s /home/otherfolder/ ./
mv ./otherfolder blog

Note that if you get a 404, you may need to give the nginx user access to the directories.