0

我正在使用 Rails 3.2 + nginx + 独角兽。我的 Rails 应用程序也运行良好。
自从我从 Apache 切换到 nginx 后,我现在面临一个问题。

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

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

如何修改我当前的 conf 文件?

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. 正确的?

4

1 回答 1

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 后端。

于 2013-10-20T10:30:58.517 回答