Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I can't figure out how to redirect / to index.html. I've gone through the threads on serverfault and I think I've tried every suggestion including:

  • rewrite statements within location /
  • index index.html at the server level, within location / and within static content
  • moving node.js proxy statements to location ~ /i instead of within location /

Obviously something is wrong somewhere else in my configuration. Here is my nginx.conf:

worker_processes  1;

pid        /home/logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include            mime.types;
    default_type       application/octet-stream;
    sendfile           on;
    keepalive_timeout  65;

    error_log   /home/logs/error.log;
    access_log  /home/logs/access.log combined;

    include sites-enabled/*;
}

and my server config located in sites-enabled

server {
  root        /home/www/public;
  listen      80;
  server_name localhost;

  # proxy request to node
  location / {
    index index.html index.htm;
    proxy_set_header   Host             $http_host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header   X-NginX-Proxy    true;

    proxy_pass         http://127.0.0.1:3010;
    proxy_redirect     off;
    break;
  }  

  #  static content
  location ~ \.(?:ico|jpe?g|jpeg|gif|css|png|js|swf|xml|woff|eot|svg|ttf|html)$ {
    access_log  off;
    add_header  Pragma public;
    add_header  Cache-Control public;
    expires     30d;  
  }

  gzip on;
  gzip_vary on;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_min_length  1000;
  gzip_disable     "msie6";
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
}

Everything else is working just fine. Requests get proxied to node correctly and static content is served correctly. I just need to be able to forward requests made to / to /index.html.

share|improve this question
    
I'm curious as to what you are trying to accomplish with the root html in the location block? Is your index.html in /home/www/public/html? I've only used absolute paths. –  tfitzgerald Jun 19 '12 at 3:00
    
I've removed that. I was basically trying anything to get it to work and that was left over from one of the obviously failed attempts. Still no love though. –  Bill Jun 19 '12 at 3:13
    
Is there anything in the error log? Can you tell if it is even trying to hit /home/www/public/index.html? –  tfitzgerald Jun 19 '12 at 3:14
    
Nothing in the error log at all. Access log has "GET / HTTP/1.1" 404. Nothing indicates it is even trying to hit index.html. The call eventually gets forwarded to my node server which displays an error page. –  Bill Jun 19 '12 at 3:18

1 Answer 1

up vote 4 down vote accepted

Basically, the root directive (inside the location block) didn't mention the full path. Also, a ; is missing at the end of index directive (could be a typo, nginx usually catches these typos).

So, your server config located in sites-enabled would look like this (after the above two changes)...

server {
  root        /home/www/public;
  listen      80;
  server_name localhost;
  index index.html index.htm;

  # proxy request to node
  location @proxy {
    proxy_set_header   Host             $http_host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header   X-NginX-Proxy    true;

    proxy_pass         http://127.0.0.1:3010;
    proxy_redirect     off;
    break;
  }

  location / {
    try_files $uri $uri/ @proxy;
  }

# rest of the configuration
# ...
# ...

}

share|improve this answer
    
Oops, those were leftover from my last attempt. I've updated the conf file above to fix these typos. Trying to access / still leads to a 404. –  Bill Jun 19 '12 at 3:12
    
Okay, can you please put the index directive outside the location block? I'm updating my config file as well now. Thanks. –  Pothi Jun 19 '12 at 3:26
    
After seeing your other comment regarding the server log, I see what's wrong. I'm updating my config now for the updated configuration. Thanks. –  Pothi Jun 19 '12 at 3:35
    
No dice. localhost/index.html works, localhost and localhost/ still result in a 404. Access log still gives 404, error log has nothing in it. / is still getting routed directly to my node server. –  Bill Jun 19 '12 at 3:41
    
Try keeping location / { try_files $uri $uri/ @proxy; } above location @proxy directive. –  Pothi Jun 19 '12 at 3:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.