2013-12-17 30 views
3

我有以下nginx配置我想添加基本身份驗證到這個配置。如何添加基本身份驗證到一個nginx配置與重寫

upstream phpfcgi { 
     server 127.0.0.1:7777; 
# server unix:/var/run/php5-fpm.sock; #for PHP-FPM running on UNIX socket 
} 
server{ 
    listen 80; 
    server_name qu.abc.com; 
    root /home/qu/website/current/web; 
    location/{ 
# try to serve file directly, fallback to rewrite 
      try_files $uri @rewriteapp; 

    } 

    location @rewriteapp { 
    # rewrite all to app.php 
      rewrite ^(.*)$ /app.php/$1 last; 
    } 

    location ~ ^/(app|app_dev|config)\.php(/|$) { 
      fastcgi_pass phpfcgi; 
      fastcgi_split_path_info ^(.+\.php)(/.*)$; 
      include fastcgi_params; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      fastcgi_param HTTPS off; 
    } 

    error_log /var/log/nginx/project_error.log; 
    access_log /var/log/nginx/project_access.log; 
} 

我編輯的位置塊到這個

location/{ 
# try to serve file directly, fallback to rewrite 
      auth_basic "Restricted"; 
      auth_basic_user_file /home/qu/website/current/web/.htpassword; 
      try_files $uri @rewriteapp; 

    } 

但是這一次不重寫工作。 有人可以幫忙。

+0

嗨@PravinMishra我已經嘗試從中適應。我得到的問題是在服務器檢查basic_auth之前完成重寫。所以我需要通過首先檢查basic_auth來完成這項工作,然後進行重寫。或者可能有一些替代方法來做到這一點。 –

回答

1

這聽起來像你想爲整個服務器啓用基本身份驗證,而不僅僅是一個location。該location小號工作方式,是隻有一個適用的時間,因此,如果你在一個單一的location指定一個權威性的政策,但它確實rewrite其他一些location,那麼其他location不會受到來自權威性政策以前location

據爲auth_basic的文件,好像它只是允許在location背景下不使用,但在serverhttp,太。

因此,如果你希望你的整個server爲需要身份驗證,只需將您的所有auth_basic指令的一個級別,從一個單一的location是,在單一server之中。

+0

非常感謝@cnst我已經使它工作。在做你的建議時,我也注意到nginx並沒有遵循符號鏈接地址。所以我必須使用符號鏈接地址指向的實際地址。我想首先可能是我的問題的根本原因。 –