2016-04-20 9 views
0

Ich entwickle ein Projekt mit Symfony2, Nginx.Symfony2 Nginx auth_basic config nicht öffnen web assests

Projekt befindet sich in meiner Subdomain wie developing_site.mysite.com.

Ich möchte den Zugriff auf diese Subdomain ohne Authentifizierung beschränken. Nicht nur für Dev- und Config-Dateien, sondern auch für die Produktion.

Also ich habe auth_basic Komponente Nginx Config-Datei in location/ Sektor in Nginx Config hinzugefügt, die von Symfony offizielle Website empfohlen wird. Als Ergebnis, vor dem Laden der Seite Server fragt Authentifizierung und lädt alles außer für alle Dateien speichert in /web Verzeichnis wie Bilder, js, css und so weiter. Daher wird der gesamte Inhalt von .php verarbeitet, jedoch ohne Stil und dynamische Funktionalität.

Also, wie kann ich dieses Problem lösen? Was mache ich falsch?

Nginx Konfiguration sieht wie folgt aus:

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

Sie DEV und ART Umgebung mischen, können Sie nicht logisch 2-Umgebungen im selben Ordner. versuche, den gesamten #DEV-Konfigurationsblock zu entfernen und nginx neu zu laden – Miro

Antwort

0

ich allein das Problem gelöst ..

Zwei Fehler:

  1. syntaktische Fehler
  2. falschen Stelle von auth_basic Block

Der syntaktische Fehler ist in var/www/developing_site/.lock/.htpasswd;. Ich habe relative statt absolute Links verwendet. Die richtige Form ist /var/www/developing_site/.lock/.htpasswd; (sorry dafür ...)

Wenn ich auth_basic Block in location/ platziert haben habe ich gesetzt Authentifizierung nur / Lage, dass alle /web Anfragen in der Tat Prozesse ... (/ Webanfragen wasn‘ t verarbeitet wegen 1-st Fehler ...)

Hauptsymfony Anforderungen werden von location ~ ^/(app_dev|config)\.php(/|$) Block in Nginx Config-Datei verarbeitet.

Lösung: Um alle Anfragen auf irgendwelche Dateien von developing_site.mysite.com ohne Authentifizierung zu beschränken, sollte auth_basic Block vor irgendwelchen location Blöcken sein.

Also die richtige nginx Konfiguration sollte wie folgt aussieht:

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; 
} 
Verwandte Themen