Rails没有在生产中提供我的robots.txt和sitemap.xml.gz

时间:2014-04-10 04:12:53

标签: ruby-on-rails nginx

虽然这些文件在开发中很好地提供,并且在本地计算机上也在生产中,但这些文件不能在实时生产中提供。我收到404找不到错误。其他一切都很好。这些文件存在于应用程序的公共目录中(-approot- / public)

我在实时服务器上使用nginxunicorn。我的nginx/sites-available/default文件:

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;
   }

}

3 个答案:

答案 0 :(得分:2)

您再写一个位置规则,例如

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;

}

}

答案 1 :(得分:0)

将此附加到etc/nginx/sites-available/default

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

答案 2 :(得分:0)

将此附加到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;
}