Nginx auth_basic not working for a specific url - authentication

I would like to password protect one of the URLs I have and I am trying to do it with:
location /about/payment {
auth_basic "secured site";
auth_basic_user_file /var/www/my.passwd;
}
The problem is that I am asked for the username and paasword. AS soon as I put the right username and password, I am getting a 404 Error with this log:
*55268 open() "/var/www/mysite.com/deployment/web/about/payment" failed (2: No such file or directory), client: 172.16.0.53, server: ~^(?<branch>\w+)\.mysite\.dev$, request: "GET /about/payment HTTP/1.1", host: "deployment.mysite.dev"
EDIT:
The entire nginx conf file is here
server {
listen 80;
access_log ...;
error_log ...;
server_name ~^(?<branch>\w+)\.mysite\.dev$ ~^(?<branch>\w+)\.mysite\.com$;
root /var/www/git/branches/mysite.com/$branch/web;
location /about/payment {
auth_basic "secured site";
auth_basic_user_file /var/www/mysite.passwd;
}
# strip app_eudev.php/ prefix if it is present
rewrite ^/app_eudev\.php/?(.*)$ /$1 permanent;
# remove trailing slash
rewrite ^/(.*)/$ /$1 permanent;
# sitemap rewrite
rewrite ^/sitemap_(.*)$ /sitemap/$1 last;
location / {
try_files $uri #symfonyapp;
}
location #symfonyapp {
rewrite ^(.*)$ /app_eudev.php/$1 last;
}
location /var/www/dms/ {
internal;
alias /var/www/dms/;
}
location #htmlimages {
root /var/www/dms/;
}
location ~ /html/.*\.(png|gif|jpg|pdf)$ {
root ...;
try_files $uri #htmlimages;
}
location /files {
root ...;
}
location /assets {
root ...;
}
location /img {
root ...
}
location ~ \.php(/|$) {
fastcgi_pass 127.0.0.1:9001;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}

When nginx find matching location that will process request it ignores any other location that could possibly match request.
In your case before you add auth, request to /about/payment was proceeded by location / which finally passed request to PHP. But as soon as you add location /about/payment request to that URL will be processed by this location which has no special directives so nginx will try to serve static files.
You should add directives that pass request to PHP, in this case it's really simple:
location /about/payment {
auth_basic "secured site";
auth_basic_user_file /var/www/my.passwd;
root ...;
try_files $uri #symfonyapp;
}

E.g. for Wordpress site, I wanna lock the URL
location ~* /block-url/ {
auth_basic "Internal Staging Site";
auth_basic_user_file /etc/nginx/.htpasswd;
try_files $uri $uri/ /index.php?$args; # Most important!
}

Related

Magento 1.9 pages not loading without index.php

When I enable ssl on the website the pages won't load without index.php in the url. I have set Use Web Server Rewrites, Use Secure URLs in Frontend, and Use Secure URLs in Admin to Yes. Offloader header is set to SSL_OFFLOADED. I have cleared the cache on the website and my browser. I have checked phpinfo() and mod_rewrite is enabled. The secure and unsecure url both have https://. I have also replaced the .htaccess file with a fresh copy and it different make any difference.
That is what I have tried from various posts on Stackoverflow and I am unsure what to try next.
Are you using Apache or Nginx? Nginx does not read/use the .htaccess file. You need to use something like this in Nginx config:
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 14d;
log_not_found off;
}
location / {
index index.php;
try_files $uri $uri/ #handler;
}
location #handler {
rewrite / /index.php;
}
location ^~ /app/ { deny all; }
location ^~ /includes/ { deny all; }
location ^~ /lib/ { deny all; }
location ^~ /media/downloadable/ { deny all; }
location ^~ /pkginfo/ { deny all; }
location ^~ /report/config.xml { deny all; }
location ^~ /var/ { deny all; }
location /var/export/ {
auth_basic 'Restricted';
auth_basic_user_file htpasswd;
autoindex on;
}
location /. {
return 404;
}
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
rewrite ^/minify/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
rewrite ^/skin/m/([0-9]+)(/.*.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;
location /lib/minify/ {
allow all;
}

Why Nginx redirect all my https request to a specific subdomain?

After installing Owncloud on my server, on a subdomain (let's say) cloud.motherboard.fr, I have the issue that all my https requests (like https://hey.motherboard.fr) redirect to the following Owncloud page :
while going to https://hey.motherboard.fr
So I guess that my Nginx configuration redirect all https connections to Owncloud. Here is my configuration file :
upstream php-handler {
server 127.0.0.1:9000;
# server unix:/var/run/php5-fpm.sock;
}
server {
listen 80;
server_name cloud.motherboard.fr; #YourIP or domain
return 301 https://$server_name$request_uri; # redirect all to use ssl
}
server {
listen 443 ssl;
server_name cloud.motherboard.fr; #YourIP or domain
#SSL Certificate you created
ssl_certificate /etc/nginx/cert/owncloud.crt;
ssl_certificate_key /etc/nginx/cert/owncloud.key;
# owncloud path
root /var/www/cloud/owncloud/;
client_max_body_size 10G; # set max upload size
fastcgi_buffers 64 4K;
rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
index index.php;
error_page 403 /core/templates/403.php;
error_page 404 /core/templates/404.php;
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ ^/(data|config|\.ht|db_structure\.xml|README) {
deny all;
}
location / {
# The following 2 rules are only needed with webfinger
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
try_files $uri $uri/ index.php;
}
location ~ ^(.+?\.php)(/.*)?$ {
try_files $1 = 404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$1;
fastcgi_param PATH_INFO $2;
fastcgi_param HTTPS on;
fastcgi_pass php-handler;
}
# Optional: set long EXPIRES header on static assets
location ~* ^.+\.(jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
expires 30d;
# Optional: Don't log access to assets
access_log off;
}
}
It looks like it comes from the two first server block, but I didn't managed to change it. Can it have a link with php-pfm ?
My hey.motherboard.fr configuration is pretty simple :
server {
server_name hey.motherboard.fr;
location / {
root /var/www/hey;
index index.html index.htm;
}
}

Webservice doens't working on fresh install

After performing a fresh install of Prestashop ( v 1.6.0.9 ) i've encountered some problems about making the webservice feature avaliable.
I have set the key and i'm able to check the ws's avaliable using the url
http://example.com/webservice/dispatcher.php?ws_key=my_key
And 3 of the relevant result's are:
http://example.com/api/employees?schema=synopsis
http://example.com/api/employees
http://example.com/api/employees?schema=blank
So, testing the links above,i receive the message This page is not available right into my face, i'm not sure why this behaviour.
For aditional information, i followed the steps on the Web Service Tutorial where i downloaded the PSWebServiceLibrary.php file into my root folder, i also created a test file with the content:
<?php
/**
* Created by PhpStorm.
* User: thales.pereira
* Date: 05/01/15
* Time: 16:10
*/
require_once( './PSWebServiceLibrary.php' );
$shop_url="http://localhost";
$secret_key="the_key";
$debug=false;
try {
$webService = new PrestaShopWebservice($shop_url, $secret_key, $debug);
$opt['resource'] = 'customers?schema=synopsis';
$xml = $webService->get($opt);
echo $xml;
}
catch (PrestaShopWebserviceException $ex) {
echo 'Other error: <br />' . $ex->getMessage();
}
But well.. the result was:
Other error:
This call to PrestaShop Web Services failed and returned an HTTP status of 404.
That means: Not Found.
For this dev environment, i'm using MAMP Version 3.0.7.3
The main problem was with nginx regex rules.
Here is what is currently working for me:
server {
listen 80;
server_name 127.0.0.1;
access_log /var/log/nginx/prestashop.access.log;
error_log /var/log/nginx/prestashop.error.log;
root /var/www/prestashop;
if ($http_host != "127.0.0.1") {
rewrite ^ http://127.0.0.1$request_uri permanent;
}
index index.php index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
rewrite ^/([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2$3.jpg last;
rewrite ^/([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3$4.jpg last;
rewrite ^/([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4$5.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg last;
rewrite ^/c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2$3.jpg last;
rewrite ^/c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
rewrite ^/images_ie/?([^/]+)\.(jpe?g|png|gif)$ /js/jquery/plugins/fancybox/images/$1.$2 last;
try_files $uri $uri/ /index.php$is_args$args;
error_page 404 /index.php?controller=404;
location ~* \.(gif)$ {
expires 2592000s;
}
location ~* \.(jpeg|jpg)$ {
expires 2592000s;
}
location ~* \.(png)$ {
expires 2592000s;
}
location ~* \.(css)$ {
expires 604800s;
}
location ~* \.(js|jsonp)$ {
expires 604800s;
}
location ~* \.(js)$ {
expires 604800s;
}
location ~* \.(ico)$ {
expires 31536000s;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}

CakePHP Htaccess 2 Nginx rewrite

We're moving a CakePHP Framework installation to a server where there's an Nginx running. Previous server had Apache. This CakePHP has multiple sub-installations on subfolders which all include the /app/webroot/ folder. We've managed to get the index.php working but all the other files located under /app/webroot/ like javascript and CSS don't link up there.
Now, we've tried getting this to work on nginx with multiple different variations. The problem is, the site loads up PHP files and clean URL'S work. Loading CSS and JS files which are located under /app/webroot/ don't.
We're trying to set up the root to subdomain.example.com where there's an index.php with a header() function to redirect the user to a folder, where there's CakePHP. Basically multiple sites under sub folders. So the CakePHP sites are http://subdomain.example.com/subfolder
Here's the nginx conf we're trying. I've been trying various different options with no effect.
server {
rewrite ^(.*) http://example.com$1 permanent;
}
server {
listen 80;
server_name example.com www.example.com subdomain.example.com;
access_log /home/example.com/logs/access.log;
error_log /home/example.com/logs/error.log error;
root /home/example.com/public_html/;
index index.php;
gzip_static on;
location /subfolder {
root /home/example.com/public_html/subfolder/;
index index.php;
rewrite ^/subfolder/(/.*)$ /app/webroot$1 break;
try_files $uri $uri/ /subfolder/app/webroot/index.php?$args;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_comles $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/example.com-php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
app/webroot/ will be your server root. And separate location for process index.php file.
Example:
server {
listen 80;
server_name yourserver.com;
root /web/path/;
index index.php;
location / {
rewrite ^(/.*)$ /app/webroot$1 break;
try_files $uri $uri/ /app/webroot/index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

How to make Digital Access Pass sub directory (.../dap) to use http onn nginx web server, OS: Ubuntu?

I have nginx web server installed and https domain. I want to make a sub-directory /dap in root folder to use http and exclude from ssl. Please guide me through this.
My /etc/nginx/sites-available virtual host file for this domain is as below,
# WPSINGLE FAST CGI NGINX CONFIGURATION
server {
listen 198.27.70.206:80;
server_name howtofightnow.com;
return 301 https://howtofightnow.com$request_uri;
}
server {
listen 443 ssl;
server_name howtofightnow.com;
ssl on;
ssl_certificate /etc/nginx/ssl/howtofightnow_com.pem;
ssl_certificate_key /etc/nginx/ssl/server.key;
#listen 198.27.70.206:80;
#server_name howtofightnow.com;
access_log /var/log/nginx/howtofightnow.com.access.log rt_cache;
error_log /var/log/nginx/howtofightnow.com.error.log;
root /var/www/howtofightnow.com/htdocs;
index index.php index.htm index.html;
location /zabbix {
if ($scheme ~ ^http:){
rewrite ^(.*)$ https://$host$1 permanent;
}
alias /usr/share/zabbix;
index index.php;
error_page 403 404 502 503 504 /zabbix/index.php;
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
expires epoch;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass php;
}
location ~ \.(jpg|jpeg|gif|png|ico)$ {
access_log off;
expires 33d;
}
}
# Use Cached Or Actual File If They Exists, Otherwise Pass Request To WordPress
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
try_files $uri /index.php?args;
include fastcgi_params;
fastcgi_pass php;
}
location /vma {
root /usr/local/vimbadmin/public ;
try_files $uri $uri/ /index.php?$args;
location ~ .php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass php;
}
}
rewrite ^/index.php/register/thanks(.*) /register/thanks$1 permanent;
include /etc/nginx/common/locations.conf;
}
Simply add this location (/dap) to your http server section, plus add a separate root location, as following:
server {
listen 198.27.70.206:80;
server_name howtofightnow.com;
location / {
return 301 https://howtofightnow.com$request_uri;
}
location /dap/ {
# specific rules for this location go here
}
}