This page looks best with JavaScript enabled

Nginx Configuration Problem Record

1. server_name Has No Effect

Symptom: Nginx reverse-proxies two applications, and the configuration details are as follows. It turned out that requests to b.chenshaowen.com and a.chenshaowen.com both returned responses from service A.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
server {

	 listen 80;
	 server_name a.chenshaowen.com;
	 location / {
			proxy_pass http://A;
	 }
 }

server {

	listen 8080;
	server_name b.chenshaowen.com;
	location / {
		   proxy_pass http://B;
	}
}

Cause: when none of the server rules match, Nginx falls back to the first server block.

Solution: add a server block at the very top.

1
2
3
4
5
6
server {
	  listen 80;
	  server_name XX.chenshaowen.com;
	  return 404;
 }
 # 其他 server

2. Adding Password Authentication to Nginx

1.Install the password generation tool

1
yum install httpd-tools

2.Generate the account and password

1
htpasswd -bc /your/password/file/path username password

3.Nginx configuration

1
2
3
4
5
location / {
	   auth_basic "input password";
	   auth_basic_user_file  /your/password/file/path;
	   ...
 }

4.Restart the Nginx service

1
2
3
4
nginx -s reload
```

On the next visit, Nginx pops up a small dialog asking for the password. Log in with username:password.

WeChat Official Account
WRITTEN BY
WeChat Official Account