Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Protect Sensitive Pages With Nginx
coder7475
coder7475

Posted on

     

Protect Sensitive Pages With Nginx

Security features in NGINX

  • Restrict access wherever possible by allowing and blocking IP address.

  • You can protect sensitive pages by username and password

  • Use SSL certificate to secure site by encrypting client-server traffic

Restrict Access Via IP address

Let's say you wanna restrict all api address fromm accessing a page calledsecure.

To do that in your nginx configuration add a location block inside your server context.

server{  ...  location /secure{    try_files$uri /secure.html;    deny all;}  ...}
Enter fullscreen modeExit fullscreen mode

Thedeny all makes all ip address restricted.

Now test your nginx config and reload. Run:

sudonginx-tsudonginx-s reload
Enter fullscreen modeExit fullscreen mode

Try to access/secure path you will get a 403 Forbidden HTTP Status Page.

If you wanna allow any ip address use the below syntax in config file:

server{  ...  location /secure{    try_files$uri /secure.html;    allow 127.0.0.1;    deny all;}  ...}
Enter fullscreen modeExit fullscreen mode

change127.0.0.1 to the ip address you want to allow.

If you want to know your own ip address. Go to google and searchwhat is my ip address. You will get your ip address from search result.

Note: Your IP address is generally a public ip address provided by your ISP provider. It usually changes every one or two days.

Protect sensitive pages by username and password

Sometimes we want allow internal members to access some page. In that case, we can setup a username and password to allow only internal members.

Steps to setup username and password for a page:

  1. First install a utility to set user name and password:
sudoapt-getinstall-y apache2-utils
Enter fullscreen modeExit fullscreen mode
  1. Next switch to root user if you are not already. Run:
sudosu
Enter fullscreen modeExit fullscreen mode

Enter your password if terminal asks for one.

  1. For first time to set a username password.Syntax:
  htpasswd-c /path/to/save/passwords username
Enter fullscreen modeExit fullscreen mode

Example:

  htpasswd-c /etc/nginx/password admin
Enter fullscreen modeExit fullscreen mode

The above command create a user namedadmin. The terminal will asks you for new password. Type the new password you want to set.

  1. If you to add another user calleduser1 type:
  htpasswd /etc/nginx/password user1
Enter fullscreen modeExit fullscreen mode

Note: no need for-c flag

  1. Now let's say we want to set auth forsecure page. Go to your nginx config file and use:
# my file path: /etc/nginx/conf.d/tech.conf# your might be in: /etc/nginx/sites-available/tech.confserver{  ...  location /secure{        try_files$uri /secure.html;        auth_basic"Authentication is required here...";        auth_basic_user_file /etc/nginx/passwords;        ....}   ....}
Enter fullscreen modeExit fullscreen mode
  1. Now test and reload your nginx config file. Run:
sudonginx-tsudonginx-s reload
Enter fullscreen modeExit fullscreen mode
  1. Go to secure page and you will see, it is asking you for username and password.

References

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a Software Engineer focusing on web development. I am currently exploring the world of DevOps Engineering.
  • Joined

More fromcoder7475

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp