Skip to content

Instantly share code, notes, and snippets.

@vtr0n
Created May 14, 2019 12:20
Show Gist options
  • Save vtr0n/0eb128cd556261d953c9f63007b6a76b to your computer and use it in GitHub Desktop.
Save vtr0n/0eb128cd556261d953c9f63007b6a76b to your computer and use it in GitHub Desktop.
nginx http auth with cookies
# create the password file for user 'admin'
sudo htpasswd -c /etc/nginx/.htpasswd admin
# Admin Panel
location /Admin/ {
set $auth_basic "closed site";
if ($cookie_adminCookie = "secretCookieValue") {
set $auth_basic off;
}
auth_basic $auth_basic;
auth_basic_user_file /etc/nginx/.htpasswd;
error_page 404 = @admin;
}
location @admin {
root /var/www;
# you can use rewrite here, for example:
# rewrite ^/Admin/(.*)$ /public/index.php?Admin/$1 break;
# set php fascgi params:
# fastcgi_pass unix:/run/php/php7.2-fpm.sock;
# include fastcgi_params;
# fastcgi_param SCRIPT_FILENAME $request_filename;
# set cookie life for 1 year
add_header Set-Cookie "adminCookie=secretCookieValue;max-age=3153600000;path=/" always;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment