Я ' Я установил базовую авторизацию на некоторых страницах своего сайта. Журналы сервера показывают, что аутентификация работает, однако, как только я вошел в систему, все защищенные страницы возвращают ошибку 404 - и не мою фактическую страницу 404, а общую страницу Nginx 404.
Это приложение Django, работающее на Heroku. На моем сервере разработки, на котором отключена базовая аутентификация, страницы загружаются правильно, поэтому с моими маршрутами все в порядке.
Вот мой nginx.conf.erb:
daemon off;
#Heroku dynos have at least 4 cores.
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;
events {
use epoll;
accept_mutex on;
worker_connections 1024;
}
http {
gzip on;
gzip_comp_level 2;
gzip_min_length 512;
server_tokens off;
include mime.types;
default_type application/octet-stream;
sendfile on;
client_body_timeout 5;
upstream app_server {
server unix:/tmp/nginx.socket fail_timeout=0;
}
server {
listen <%= ENV["PORT"] %>;
server_name _;
keepalive_timeout 5;
root /app;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
location /protected {
try_files $uri $uri/ =404;
auth_basic "Restricted";
auth_basic_user_file /app/config/.htpasswd;
}
}
}
Вот вывод журнала, когда я пытаюсь получить доступ к защищенной странице после входа в систему:
2018-06-19T06:58:10.612050+00:00 heroku[router]: at=info method=GET path="/protected/" host=mysite.com request_id=fe3aedbc-f31b-4012-9084-6ec7c194583d fwd="183.89.28.135,172.68.6.102" dyno=web.1 connect=1ms service=2ms status=404 bytes=707 protocol=https
2018-06-19T06:58:10.611886+00:00 app[web.1]: measure#nginx.service=0.001 request="GET /protected/ HTTP/1.1" status_code=404 request_id=fe3aedbc-f31b-4012-9084-6ec7c194583d remote_addr="10.158.185.56" forwarded_for="183.89.28.135, 172.68.6.102" forwarded_proto="https" via="1.1 vegur" body_bytes_sent=564 referer="-" user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.25 Safari/537.36"
You probably need to repeat proxy_pass
inside the second location
block:
location /protected {
try_files $uri $uri/ =404;
proxy_pass http://app_server; # add this line
auth_basic "Restricted";
auth_basic_user_file /app/config/.htpasswd;
}
I've had the same problem with a configuration similar to yours. To investigate the issue, I've opened the nginx logs (tail -f /var/log/nginx/error.log
). There, I've seen a couple of open() "/usr/share/nginx/html/my/endpoint/path" failed (2: No such file or directory)
– this suggested that nginx doesn't perform reverse proxy without explicit proxy_pass in each location block.
В моем случае ошибка 404 возникла из-за того, что мой корневой каталог вернулся к состоянию по умолчанию. Для Debian я думаю, что корневой каталог по умолчанию — /usr/share/nginx/html
location ... {
# Forgot to set the root...
root /home/www-data/example.com/restricted;
auth_basic "Restricted Area";
auth_basic_user_file thepass;
}
. Возможно, я мог бы вложить этот блок местоположения в основной блок местоположения (где у меня установлен корневой каталог), но решил оставить его отдельным, как здесь. ..