大网数据广告活动
服务器运维问答

Nginx location allow ip not working as expected

· 52CE 我爱测

i need your help with setting location allow, location /route { deny [my-ip]; } So this works, it doesn't let me access the route Throws this error 403 Forbidden nginx/1.10.0 (Ubuntu) And this... location /route { allow [my-ip]; …

问题原文

原帖链接 · Janva · CC BY-SA 3.0

i need your help with setting location allow,

location /route {
    deny [my-ip];
}

So this works, it doesn't let me access the route

Throws this error

403 Forbidden
nginx/1.10.0 (Ubuntu)

And this...

location /route {
    allow [my-ip];
    deny all;
}

Doesn't let me access but it's supposed to let me access the route, can't understand why, it shows this error

404 Not Found
nginx/1.10.0 (Ubuntu)

Config file (with two examples on routes):

server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/laravel/public;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;

server_name [my-domain];

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ /index.php?$query_string;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    include snippets/fastcgi-php.conf;
#
#   # With php7.0-cgi alone:
#   fastcgi_pass 127.0.0.1:9000;
#   # With php7.0-fpm:
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
    deny all;
}

# Phpmyadmin Configurations
location /phpmyadmin {
   root /usr/share/;
   index index.php index.html index.htm;
   location ~ ^/phpmyadmin/(.+\.php)$ {
           try_files $uri =404;
           root /usr/share/;
           #fastcgi_pass 127.0.0.1:9000;
           #fastcgi_param HTTPS on; # <-- add this line
           fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
           fastcgi_index index.php;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
   }
   location ~* ^/phpmyadmin/(.+\.
(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
           root /usr/share/;
   }
}

# Dealing with the uppercased letters
location /phpMyAdmin {
   rewrite ^/* /phpmyadmin last;
}

location /logs {
    deny [myip];
}

location /admin {
    allow [myip];
    deny all;
}
}

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

I am not convinced that this is particularly secure as the /index.php script remains unprotected. But you are overriding the try_files statement in the location / block, so you should add it to the new location block:

location /admin {
    allow [my-ip];
    deny all;

    try_files $uri $uri/ /index.php?$query_string;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-04-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。