Nginx在访问location和localhost时出现403禁止访问错误。

3
我的目标是使用SSH隧道访问特定位置(即phpmyadmin),地址为http://localhost/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 Forbidden nginx/1.17.10 (Ubuntu)

为了测试,我已经删除了 "deny all;",一切都正常工作,但每个ip地址都可以访问phpmyadmin位置。

nginx的错误日志:

2020/05/05 23:52:13 [error] 21905#21905: *1 access forbidden by rule, client: ::1, server: _, request: "GET /phpmyadmin/ HTTP/1.1", host: "localhost"

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个回答

3

你需要允许 ::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 - Dilson Rainov
仍然没有任何结果。我已经尝试了另一个位置:location ^~ /test {satisfy all; #需要满足两个条件allow 127.0.0.1; #仅允许本地主机拒绝所有其他来源} ,但结果仍然相同。 - Steve
抱歉打扰,您是重新加载还是重启了nginx?nginx -t的结果是什么? - Dilson Rainov
我稍后会提供一个有用的测试...很抱歉现在无法提供帮助。 - Dilson Rainov
@Steve 你测试过了吗? - Dilson Rainov
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接