I'm trying to achieve following thing. I've main website under domain root directory. It's working fine. i.e PHP files are executed. After some time I've added "auth" directory with basic HTTP authentication. Next, I uploaded PHP program into "auth" directory, but PHP files are downloading instead of executing.
- domain.name/test.php -> executing
- domain.name/sub/file.php -> executing
- domain.name/auth/protected.php -> downloading instead of executing
Here is sites-available conf
server { charset utf-8; listen 80 default_server; listen [::]:80 default_server; root /var/www/html; # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html; server_name xxx.com; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. # try_files $uri $uri/ =404; try_files $uri $uri/ /index.php?$args; } location ^~ /auth { auth_basic "Restricted Content"; auth_basic_user_file /var/www/html/auth/.htpasswd; } location ^~ /protected { deny all; } location ~ \.php$ { include snippets/fastcgi-php.conf; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; } location ~ /\.(ht|git|svn) { deny all; } }
Currently I've following solution, but I believe it's not a right way.
location ^~ /auth { auth_basic "Restricted Content"; auth_basic_user_file /var/www/html/auth/.htpasswd; location ~ \.php$ { include snippets/fastcgi-php.conf; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; } }
Following solution doesn't work
location ^~ /auth { auth_basic "Restricted Content"; auth_basic_user_file /var/www/html/auth/.htpasswd; try_files $uri @php-fpm; } location @php-fpm { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php5-fpm.sock; }