2016-04-20 41 views
0

我正在開發一個使用Symfony2,Nginx的項目。Symfony2 Nginx的auth_basic配置不打開網頁資源

項目位於像我的子域名develop_site.mysite.com。

我想在沒有身份驗證的情況下限制對此子域的訪問。不僅要開發和配置文件,還要生產。

所以我添加了auth_basic組件到nginx配置文件中的location/扇區,在symfony官方網站推薦的nginx配置中。 因此,在頁面加載服務器要求身份驗證並加載除/web目錄(如圖像,js,css等)中的任何文件存儲以外的所有內容。因此,所有內容都由.php處理,但沒有任何樣式和動態功能。

那麼我該如何解決這個問題?我做錯了什麼?

Nginx的配置是這樣的:

server { 

listen {MyServerIp}; 
server_name developing_site.mysite.com; 

root /var/www/developing_site/web; 
index index.php index.html index.htm; 

location/{ 
    try_files $uri /app.php$is_args$args; 
    auth_basic "Restricted Content"; 
    auth_basic_user_file var/www/developing_site/.lock/.htpasswd; 
} 

# DEV 
# This rule should only be placed on your development environment 
# In production, don't include this and don't deploy app_dev.php or config.php 
location ~ ^/(app_dev|config)\.php(/|$) { 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_split_path_info ^(.+\.php)(/.*)$; 
    include fastcgi_params; 
    # When you are using symlinks to link the document root to the 
    # current version of your application, you should pass the real 
    # application path instead of the path to the symlink to PHP 
    # FPM. 
    # Otherwise, PHP's OPcache may not properly detect changes to 
    # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126 
    # for more information). 
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; 
    fastcgi_param DOCUMENT_ROOT $realpath_root; 
} 
# PROD 
location ~ ^/app\.php(/|$) { 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_split_path_info ^(.+\.php)(/.*)$; 
    include fastcgi_params; 
    # When you are using symlinks to link the document root to the 
    # current version of your application, you should pass the real 
    # application path instead of the path to the symlink to PHP 
    # FPM. 
    # Otherwise, PHP's OPcache may not properly detect changes to 
    # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126 
    # for more information). 
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; 
    fastcgi_param DOCUMENT_ROOT $realpath_root; 
    # Prevents URIs that include the front controller. This will 404: 
    # http://domain.tld/app.php/some-path 
    # Remove the internal directive to allow URIs like this 
    internal; 
} 

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

你混合DEV和PROD環境中,你不能在邏輯上具有相同的文件夾2度的環境。嘗試刪除整個#DEV配置塊並重新加載nginx – Miro

回答

0

我自己解決了問題..

兩個錯誤:

  1. 語法錯誤
  2. 不正確的地方的auth_basic

語法錯誤在var/www/developing_site/.lock/.htpasswd;。我用相對鏈接而不是絕對的。正確的方式是/var/www/developing_site/.lock/.htpasswd;(對不起,那個...)

當我放置auth_basiclocation/我設置身份驗證才/位置,實際上處理所有/web請求......(/ Web請求WASN」 t因爲第一次錯誤而被處理...)

symfony的主要請求是由nginx配置文件中的location ~ ^/(app_dev|config)\.php(/|$)塊處理的。

解決方案:要限制對沒有身份驗證的任何developers_site.mysite.com文件的任何請求,auth_basic塊應放置在任何location塊之前。

因此正確nginx的配置應該是這樣的:

server { 

listen MyServerIp; 
server_name developing_site.mysite.com; 

auth_basic "Unauthorized"; 
auth_basic_user_file /var/www/.lock/.htpasswd; 

root /var/www/developing_site/web; 
index index.php index.html index.htm; 

location/{ 
    try_files $uri /app.php$is_args$args; 
} 

# DEV 
# This rule should only be placed on your development environment 
# In production, don't include this and don't deploy app_dev.php or config.php 
location ~ ^/(app_dev|config)\.php(/|$) { 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_split_path_info ^(.+\.php)(/.*)$; 
    include fastcgi_params; 
    # When you are using symlinks to link the document root to the 
    # current version of your application, you should pass the real 
    # application path instead of the path to the symlink to PHP 
    # FPM. 
    # Otherwise, PHP's OPcache may not properly detect changes to 
    # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126 
    # for more information). 
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; 
    fastcgi_param DOCUMENT_ROOT $realpath_root; 
} 
# PROD 
location ~ ^/app\.php(/|$) { 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    fastcgi_split_path_info ^(.+\.php)(/.*)$; 
    include fastcgi_params; 
    # When you are using symlinks to link the document root to the 
    # current version of your application, you should pass the real 
    # application path instead of the path to the symlink to PHP 
    # FPM. 
    # Otherwise, PHP's OPcache may not properly detect changes to 
    # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126 
    # for more information). 
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; 
    fastcgi_param DOCUMENT_ROOT $realpath_root; 
    # Prevents URIs that include the front controller. This will 404: 
    # http://domain.tld/app.php/some-path 
    # Remove the internal directive to allow URIs like this 
    internal; 
} 

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