2

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;
}
}
  • 2
    It's not denying you access. 404 Not Found means you have access, but URI matching failed. What URL did you use that gave you the 404? – Diogenes deLight Apr 22 '17 at 5:54
  • As mentionned by @Diogenes deLight, 404 means something different than 403 (see en.wikipedia.org/wiki/List_of_HTTP_status_codes). Your nginx setup is working fine - I think your question might be a case of the XY problem - the issue isn't the allow/deny, but the fact you are getting a 404 (not found) for certain requests. As you don't share what request triggered the 404, it isn't clear if your issue is the request, or the setup. – iwaseatenbyagrue Apr 22 '17 at 11:24
1

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;
}
|improve this answer|||||
  • That worked, thanks! Do you know another way of doing this? Why it's not secure? Your help is really appreciated, thanks – Janva Apr 22 '17 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.