4

I've read the docs, scanned examples, and I cannot for the life of me figure out why this isn't working. I'm totally new to nginx, so if this is a stupid simple answer, go easy on me, please.

user www-data; worker_processes 4; pid /var/run/nginx.pid; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## #include /etc/nginx/conf.d/*.conf; #include /etc/nginx/sites-enabled/*; server { listen 80; server_name localhost domain.com; root /home/TMlabs/server_files; index index.html index.htm; location = /othersite { root /home/TMlabs/server_files/location2; index index.html index.htm; } location / { root /home/TMlabs/server_files/location1; index index.html index.htm; } } 

If I need to include more, let me know and I'll dump the whole nginx.conf. It's pretty basic, though.

All I'm trying to achieve is mapping domain.com to the root specified in location / (which is working) and mapping domain.com/othersite to the root specified in that directive. For some reason, it returns the /home/TMlabs/server_files/location1/index.html document when i navigate to domain.com/othersite rather than the index.html file from the /home/TMlabs/server_files/location2 folder. I've tried removing the equals and using the different operators available for matching, but nothing seems to work. it's probably something pretty basic that I am misunderstanding, being new to this stuff. I also suck at regexp. anyone care to enlighten?

edit: I think what is happening is that it is actually ignoring my directives entirely and is simply using /usr/share/nginx/wwwas the document root. I don't see this config anywhere; what am I missing?

4
  • I think you may have oversimplified your example config as there must be something else going on there to get that result. Please post it in more detail. Commented Aug 25, 2012 at 18:50
  • added the rest. i'm working from a template, and that's the rest of it. Commented Aug 27, 2012 at 19:37
  • in the full config you posted above you're missing the closing bracket for your http block. Also the index directive is inherited, so no need to repeat it in each location block. And the =/othersite means that only the specific /othersite url is matched, not any subpages which is probably not what you want. Commented Aug 28, 2012 at 11:50
  • the http block is just a typo. not like that in the actual file. whoops. Commented Aug 28, 2012 at 16:39

2 Answers 2

3

The = prefix means that the location specified should exactly match the requested URL. If you look up the error log, you will see that Nginx tries to serve the file [...]/location2/otherside, and not [...]/location2/index.{html,htm}. (full paths omited for simplicity)

index directives are a noop inside an exact-match location.

To work around this, you can either rename your file to otherside and have this configuration

location = /otherside { root /home/TMlabs/server_files/location2; } 

or use the alias directive

location = /otherside { alias [...]/location2/index.html; } 

PS: Nginx will try to figure the Content-Type of the response by the extension of the URL. Since /otherside does not have an extension, it will use application/octet-stream causing the browser to try and download the content.

You can either include the .html extension, or use the default_type directive inside location

default_type "text/html"; 
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the clarification! and the extra tidbits. My problem ended up being something rather more simple, I discovered upon finding the error.log file. (File path mangling.) But these points do answer my initial question and are very helpful! Thanks Dan, haha!
0

the following should do the trick:

server { listen 80; server_name localhost domain.com; root /home/TMlabs/server_files; index index.html index.htm; location / { try_files location1/$uri location1/$uri/; } location /othersite { try_files location2/$uri location2/$uri/; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.