Nginx 403 禁止位置和本地主机
Posted
技术标签:
【中文标题】Nginx 403 禁止位置和本地主机【英文标题】:Nginx 403 Forbidden for location and localhost 【发布时间】:2020-08-20 16:11:58 【问题描述】:我的目标是使用 SSH 隧道 http://localhost/phpmyadmin 访问特定位置(即 phpmyadmin)
我刚刚安装了带有 nginx 的 Ubuntu 20.04。以下配置在 Ubuntu 18.04 上运行良好。
我编辑了 /etc/nginx/sites-available/default 添加:
location /phpmyadmin
#Allow localhost
allow 127.0.0.1;
#deny all the others ip
deny all;
当我访问http://localhost/phpmyadmin 时收到错误消息:
403 禁止 nginx/1.17.10 (Ubuntu)
只是为了测试,我删除了“全部拒绝;”一切正常,但每个 ip 地址都可以访问 phpmyadmin 的位置。
错误日志nginx:
2020/05/05 23:52:13 [错误] 21905#21905: *1 访问被规则禁止,客户端::::1,服务器:_,请求:“GET /phpmyadmin/ HTTP/1.1”,主机:“本地主机”
server
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.html index.htm index.nginx-debian.html index.php;
server_name _;
location /
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# pass PHP scripts to FastCGI server
#
location ~ \.php$
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
location /phpmyadmin
satisfy all;
allow 127.0.0.1;
deny all;
知道为什么此配置不再适用于 ubuntu 20.04 和 nginx 1.17.10 吗?
【问题讨论】:
【参考方案1】:您也需要允许 ::1... 并在 location 块中添加 php 的参数。
这样试试
server
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.html index.htm index.nginx-debian.html index.php;
server_name _;
location /
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# pass PHP scripts to FastCGI server
#
location ~ \.php$
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
location ^~ /phpmyadmin/
allow 127.0.0.1;
allow ::1;
deny all;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
【讨论】:
我可以看到你的整个虚拟主机吗? 我已将完整配置添加到虚拟主机。当我删除“全部拒绝”时,它正在工作。 尝试将位置更改为location ^~ /phpmyadmin
还是什么都没有。我尝试了另一个位置:location ^~ /test satisfy all; #requires both conditions allow 127.0.0.1; #allow localhost only deny all; #deny all other sources
,但结果仍然相同。
不好意思问了,你是重新加载还是重启nginx? nginx -t
的结果是什么以上是关于Nginx 403 禁止位置和本地主机的主要内容,如果未能解决你的问题,请参考以下文章