如何仅针对特定URL关闭独角兽?

时间:2013-10-20 10:00:27

标签: ruby-on-rails ruby-on-rails-3 unicorn

我正在使用Rails 3.2 + nginx + unicorn。 我的rails应用程序也运行良好。
我现在面临一个问题,因为我从Apache切换到nginx。

对于特定的URL,我想禁用unicorn,并使其直接访问目标PHP页面。

我曾经在用户访问基于PHP的foo-sample.com/phpmyadmin时禁用乘客。

如何修改我当前的配置文件?

etc / nginx / conf.d / rails.conf < =我应该添加什么?

upstream sample {
    ip_hash;
    server unix:/var/run/unicorn/unicorn_foo-sample.sock fail_timeout=0;
}

server {
    listen 80;
    server_name foo-sample.com;
    root /var/www/html/foo-sample/public;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://sample;
            break;
        }
    }

    location ~ ^/assets|system/ {
        expires 1y;
        add_header Cache-Control public;
        log_not_found off;
    }
}

当我使用乘客时,我正在设置这样的conf文件。

/etc/httpd/conf/httpd.conf中

<VirtualHost *:80>

    ServerName foo-sample.com
    DocumentRoot /var/www/html/foo-sample/public

    <Directory /var/www/html/foo-sample/public>
        AllowOverride all
        Options -MultiViews
    </Directory>  

    <Location /phpmyadmin>
        PassengerEnabled off
   </Location>   

</VirtualHost>

/etc/httpd/conf.d/phpmyadmin.conf

Alias /phpmyadmin/ "/usr/share/phpMyAdmin/"
Alias /phpmyadmin "/usr/share/phpMyAdmin/"

<Directory "/usr/share/phpMyAdmin/" >

   AllowOverride all

</Directory>

试验

.
.
.
location /phpmyadmin {
    root /usr/share/phpMyAdmin;
    index index.php index.htm index.html;
    location ~ ^/phpmyadmin/(.+\.php)$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass unix:/path/to/your/php-fpm/socket;
    }
}
.
.
.

使用此代码,如果我访问foo-sample.com/phpmyadmin,则可以访问/usr/share/phpMyAdmin/index.php。正确?

1 个答案:

答案 0 :(得分:2)

尝试以这种方式重构rails.conf:

upstream sample {
    ip_hash;
    server unix:/var/run/unicorn/unicorn_foo-sample.sock fail_timeout=0;
}

server {
    listen 80;
    server_name foo-sample.com;
    root /var/www/html/foo-sample/public;
    try_files $uri $uri/index.php $uri/index.html @unicorn;

    location @unicorn {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://sample;
    }

    location ~ ^/assets|system/ {
        expires 1y;
        add_header Cache-Control public;
        log_not_found off;
    }

    location /phpmyadmin {
        alias /usr/share/phpMyAdmin/;
        index index.php;
        location ~ ^/phpmyadmin(/.+\.php)$ {
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /usr/share/phpMyAdmin$1;
            fastcgi_pass unix:/path/to/your/php-fpm/socket;
        }
    }
}

所以基本上你应该使用Nginx try_files指令。在这个特定的例子中,它将首先尝试处理静态和PHP文件。如果没有这样的文件 - 请求将被传递给Unicorn后端。