Nginx 为除 root 之外的 rails 应用程序提供 404 错误

Nginx 为除 root 之外的 rails 应用程序提供 404 错误

我有一台 Ubuntu 12.04 LTS VPS 服务器,它使用 Nginx 提供静态网站。我想设置一个可从子文件夹“foo”访问的 rails 应用程序。我使用 Passenger 来提供 rails 应用程序

这就是我配置 Nginx 的方式:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    passenger_root /home/akarki/.rvm/gems/ruby-1.9.3-p429/gems/passenger-4.0.5;
    passenger_ruby /home/akarki/.rvm/wrappers/ruby-1.9.3-p429/ruby;

    server_names_hash_bucket_size 64;

    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;
    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_types text/plain application/xml text/css text/js text/xml application/x-javascript text/javascript application/json application/xml+rss;
    charset UTF-8;
    error_log /opt/nginx/logs/file.log warn;

    server {
        listen 80;
        server_name www.domain.com;
        return 301 $scheme://domain.com$request_uri;
    }

    server {
        listen       80;
        server_name  domain.com;
        index  index.html index.htm;
        root /srv/www/domain.com;
        passenger_enabled on;
        passenger_base_uri /foo;
        try_files $uri.htm $uri.html $uri/ =404;

        location = / {
            rewrite ^ /de permanent;
        }

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

静态网站按预期运行,但唯一可访问的 rails 应用程序 URL 是 'http://domain.com/foo

任何其他 URL 都会出现 404 错误。

你对如何修复这个问题有什么建议吗?

答案1

经过大量搜索后,我发现最好的解决方案是使用 Phusion Passenger 重新安装和配置 nginx

sudo apt-get purge nginx nginx-common nginx-full
sudo apt-get install nginx
sudo nano /etc/nginx/nginx.conf
# uncomment 'passenger_root' & 'passenger_ruby' paths accordingly
sudo nano /etc/nginx/sites-enabled/default
# replace with:
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name mydomain.com;
    passenger_enabled on;
    rails_env    production;
    root         /home/deploy/myapp/current/public;

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
sudo service nginx restart

相关内容