2013-10-20 11 views
0

我正在使用Rails 3.2 + nginx +獨角獸。 我的導軌應用程序也正常工作。
自從我從Apache切換到nginx後,我現在面臨一個問題。我怎樣才能關閉獨角獸只爲特定的網址?

對於特定的URL,我想禁用獨角獸,並使其直接訪問目標PHP頁面。

當用戶訪問基於PHP的foo-sample.com/phpmyadmin時,我曾經禁用乘客。

如何修改我當前的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。對?

回答

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

+0

感謝您的回答:)什麼在'#添加代碼,使PHP後端'? – HUSTEN

+0

以及如果目標網頁不顯示延期?像'foo-sample.com/phpmyadmin'而不是'foo-sample.com/phpmyadmin/index.php' – HUSTEN

+0

檢查修改後的答案:) –