Zuhaib Ali
Zuhaib Ali

Reputation: 3384

Rails not serving my robots.txt and sitemap.xml.gz on Production

While these files are being served in development fine, and also in production on my local machine, these are not served on live production. I get a 404 not found error. Everything else is working fine. These files are present in the public directory of the app (-approot-/public)

I am using nginx and unicorn over the live server. My nginx/sites-available/default file:

upstream example.com {
 server unix:/tmp/example.socket fail_timeout=0;
}

server {
  listen 80 default;
  server_name example.com www.example.com;
  root /home/myuser/apps/example/current/public;
  access_log /var/log/nginx/access.log;
  rewrite_log on;

  location / {
    proxy_pass  http://example.com;
    proxy_redirect     off;
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    client_max_body_size       10m;
    client_body_buffer_size    128k;

    proxy_connect_timeout      90;
    proxy_send_timeout         90;
    proxy_read_timeout         90;

    proxy_buffer_size          4k;
    proxy_buffers              4 32k;
    proxy_busy_buffers_size    64k;
    proxy_temp_file_write_size 64k;
   }

  location ~ ^/(images|javascripts|stylesheets|assets|system)/  {
    root /home/myuser/apps/example/current/public;
    expires max;
    break;
   }

}

Upvotes: 0

Views: 1966

Answers (3)

hlcfan
hlcfan

Reputation: 333

Append this to etc/nginx/sites-available/{your-app-config-file}:

location ~ .*(robots.txt|sitemap.xml.gz) {
  # if you already set root above, then the following line is not needed.
  root /path_to_app/public;
}

Upvotes: 0

Jenorish
Jenorish

Reputation: 1714

You write one more location rule like

server {
  listen 80 default;
  server_name example.com www.example.com;
  root /home/myuser/apps/example/current/public;
 access_log /var/log/nginx/access.log;
 rewrite_log on;

location / {
proxy_pass  http://example.com;
proxy_redirect     off;
proxy_set_header   Host             $host;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

client_max_body_size       10m;
client_body_buffer_size    128k;

proxy_connect_timeout      90;
proxy_send_timeout         90;
proxy_read_timeout         90;

proxy_buffer_size          4k;
proxy_buffers              4 32k;
proxy_busy_buffers_size    64k;
proxy_temp_file_write_size 64k;
}

location ~ ^/(images|javascripts|stylesheets|assets|system)/  {
root /home/myuser/apps/example/current/public;
expires max;
break;
}
location ~ ^/(robots.txt|sitemap.xml.gz)/  {
root /home/myuser/apps/example/current/public;

}

}

Upvotes: 2

Zuhaib Ali
Zuhaib Ali

Reputation: 3384

Append this to etc/nginx/sites-available/default:

location ~ ^/(robots.txt|sitemap.xml.gz) {
  root /home/<user>/apps/<appname>/current/public;
}

Upvotes: 1

Related Questions