Nginx基本配置

Nginx常用命令

1
2
3
4
nginx -t #检查配置文件的语法的正确性
systemctl restart nginx #重启服务
nginx -s reload #因改变了Nginx相关配置,需要重新加载配置而重载。
systemctl enable nginx #开启自启

安装nginx

Debian/Ubuntu:

1
apt install nginx -y

Centos:

1
2
yum -y install epel-release #安装EPEL源
yum install nginx

卸载nginx:

1
apt autoremove nginx #卸载nginx

卸载nginx:(centos)

1
2
3
4
5
6
7
8
9
10
11
12
#先停止nginx
service nginx stop
#关闭自启动
chkconfig nginx off
#删除相关文件
rm -rf /usr/sbin/nginx
rm -rf /etc/nginx
rm -rf /etc/init.d/nginx
#卸载
yum remove nginx
#检查是否还有残余文件,有的话执行步骤3继续删除
whereis nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
systemctl start nginx #启动nginx
systemctl status nginx #检查服务状态
systemctl enable nginx #开启自启
systemctl disable nginx #关闭自启
systemctl stop nginx #停止nginx
systemctl restart nginx #重启服务
systemctl reload nginx #重载服务
nginx -s stop #快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。
nginx -s quit #平稳关闭Nginx,保存相关信息,有安排的结束web服务。
nginx -s reload #因改变了Nginx相关配置,需要重新加载配置而重载。
nginx -t #不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
nginx -v #显示 nginx 的版本。
nginx -V #显示 nginx 的版本,编译器版本和配置参数。

编译nginx moudles

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 首先去官网下载 http://nginx.org
wget http://nginx.org/download/nginx-1.22.0.tar.gz
tar zxvf nginx-1.22.0.tar.gz
cd nginx-1.22.0
# 输入你想要添加的module
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-openssl-opt='enable-weak-ssl-ciphers' --with-ld-opt='-ljemalloc' --with-http_realip_module
# make install会覆盖安装
make # make后会在objs下多出一个nginx
# 备份旧的nginx程序,路径可能不同,lnmp安装的路径是这样的,apt安装的路径应该是/usr/sbin/nginx
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
# 新的nginx覆盖旧的
cp objs/nginx /usr/local/nginx/sbin/nginx
# 执行不成功可以使用
sudo cp -rfp objs/nginx /usr/local/nginx/sbin/nginx
nginx -V # 检测一下是否编译成功

编译安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
./configure --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-pcre --with-http_ssl_module --with-debug --conf-path=/etc/nginx/nginx.conf \
--conf-path=/etc/nginx/nginx.conf \
--sbin-path=/usr/sbin/nginx \
--pid-path=/var/log/nginx/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--modules-path=/usr/lib/nginx/modules \

# 或者
# 路径什么的自己记住就可以了,一般放在用户目录下
./configure --prefix=/usr/local/nginx

# 编译安装nginx
make && make install
# /usr/bin和/usr/sbin是系统预装的可执行程序,会随着系统升级而改变
# /usr/local/bin目录是给用户放置自己的可执行程序的地方,推荐放在这里,不会被系统升级而覆盖同名文件。
# 定义一个软连接,类似于快捷方式。
ln -s /usr/sbin/nginx /usr/local/bin/nginx
# 如果/usr/local/bin未加入环境变量,则执行
PATH=$PATH:/usr/local/bin

可能会遇到的问题

1
2
# nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)错误
/usr/sbin/nginx -c /etc/nginx/nginx.conf # 使用nginx -c的参数指定nginx.conf文件的位置

配置文件

nginx目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|-- conf.d
| |-- demo.com.conf #个人网站配置
|-- fastcgi.conf
|-- fastcgi_params
|-- koi-utf
|-- koi-win
|-- mime.types
|-- modules-available
|-- modules-enabled
|-- nginx.conf #nginx主配置文件,不建议在里面添加网站。
|-- proxy_params
|-- scgi_params
|-- sites-available
| `-- default
|-- sites-enabled
| `-- default -> /etc/nginx/sites-available/default
|-- snippets
| |-- fastcgi-php.conf
| `-- snakeoil.conf
|-- uwsgi_params
`-- win-utf

一般的网站配置文件的命名都是网站名.conf然后放入到/etc/nginx/conf.d/下,其实不然,因为在cat /etc/nginx/nginx.conf 发现有这样一句话 include /etc/nginx/conf.d/*.conf;

其实完全可以创建新的文件夹,只需要在nginx.conf里将文件夹路径添加进去就可以正常使用。

nginx.conf只对括号敏感,不需要遵守yaml的缩进。

nginx.conf 主文件详解 点击

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
user  www www;
worker_processes 2;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 2048;
}

http {
# gzip压缩功能设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;

#获取用户真实IP,并赋值给变量$clientRealIP
map $http_x_forwarded_for $clientRealIp
{
"" $remote_addr;
~^(?P<firstAddr>[0-z\.]+),?.*$ $firstAddr;
}
#通过 map 指令,我们为 nginx 创建了一个变量 $clientRealIp ,这个就是原始用户的真实 IP 地址,不论用户是直接访问,还是通过一串 CDN 之后的访问,我们都能取得正确的原始IP地址。

#http_proxy 设置
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /usr/local/nginx/proxy_temp 1 2;
# 设定负载均衡后台服务器列表
upstream backend {
#ip_hash;
server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
}

# 很重要的虚拟主机配置
server {
listen 80;
server_name itoatest.example.com;
root /apps/oaapp;
charset utf-8;
access_log logs/host.access.log main;

#对 / 所有做负载均衡+反向代理
location / {
root /apps/oaapp;
index index.jsp index.html index.htm;
proxy_pass http://backend;
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;

}
#静态文件,nginx自己处理,不去backend请求tomcat
location ~* /download/ {
root /apps/oa/fs;

}

location ~ .*/.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /apps/oaapp;
expires 7d;
}

location /nginx_status {
stub_status on;
access_log off;
allow 192.168.10.0/24;
deny all;
}

location ~ ^/(WEB-INF)/ {
deny all;
}

#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;

location = /50x.html {
root html;
}
}
#其它虚拟主机,server 指令开始
include /etc/nginx/conf.d/*.conf;

#我们写的server{}本质是写在http{}中的,但是我们一般不在nginx.conf中更改,
#而是使用include一个新的文件夹,这样方便。因为我们更改一般都是清空后粘贴的,所以如果写在nginx.conf中更改会变得比较麻烦。
#location就是匹配url后面的目录,nginx可以直接处理静态文件。

}
1
2
3
4
5
6
7
8
9
10
location指令说明
该指令用于匹配URL。·语法如下:

location [ = | ~ | ~*| ^~] uri {
}

1= :用于不含正则表达式的uri前,要求请求字符串与uri.严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
2~:用于表示uri包含正则表达式,并且区分大小写。
3~*用于表示 uri包含正则表达式,并且不区分大小写。
4、^~:用于不含正则表达式的uri前,要求 Nginx服务器找到标识uri和请求字
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
location  = /
{
# 精确匹配 / ,主机名后面不能带任何字符串
[ configuration A ]
}

location /
{
# 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
# 但是正则和最长字符串会优先匹配
[ configuration B ]
}

location /documents/
{
# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
# 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
[ configuration C ]
}
location ~ /documents/Abc
{
# 匹配任何以 /documents/Abc 开头的地址,匹配符合以后,还要继续往下搜索,只有后面的正则表达式没有匹配到时,这一条才会采用这一条
[ configuration CC ]
}

location ^~ /images/
{ # 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
[ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$
{
# 匹配所有以 gif,jpg或jpeg 结尾的请求
# 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则
[ configuration E ]
}

location /images/
{
# 字符匹配到 /images/,继续往下,会发现 ^~ 存在
[ configuration F ]
}

location /images/abc
{
# 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
# F与G的放置顺序是没有关系的
[ configuration G ]
}

location ~ /images/abc/
{
# 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用
# 因为都是正则匹配,优先级一样,选择最上面的
[ configuration H ]
}

# location ~* /js/.*/\.js

# 优先级
( location = ) > ( location 完整路径 ) > ( location ^~ 路径 ) > ( location ,* 正则顺序 ) > ( location 部分起始路径 ) > ( / )

反代

反代http
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server
{
listen 80;
server_name demo.com;
location /
{
proxy_pass http://127.0.0.1:9091;
# 反代gpt相关服务没有打字机效果开启下面
# proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
# 此方法可能不适合域名反代
add_header X-Cache $upstream_cache_status;
add_header Cache-Control no-cache;
}
#access_log /www/wwwlogs/demo.com.log; #日志可以不写
#error_log /www/wwwlogs/demo.com.error.log;
}
反代https,这里以byr为例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
server
{
listen 80;
#listen 443 ssl;
server_name demo.com xx2.com;
#ssl_certificate crt/pem;
#ssl_certificate_key key;
#经过测试,反代https不是必须使用https,这里使用https还是为了安全而已。

#禁止搜索引擎访问
# include bloke_spyder.conf;

location ~ signup\.php { deny all; }

location /
{
proxy_pass https://byr.pt; #你想要反代的域名
proxy_set_header Host byr.pt; #这里替换一下
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Accept-Encoding "";
sub_filter "byr.pt" "demo.com"; #rewrite url 替换
sub_filter_once off;

proxy_ssl_server_name on; #反代https必开
add_header X-Cache $upstream_cache_status;
}
#access_log /www/wwwlogs/demo.com.log; #日志可以不写
#error_log /www/wwwlogs/demo.com.error.log;
}

bloke_spyder.conf,位置为 /etc/nginx/bloke_spyder.conf

1
2
3
4
if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot" ) 
{
return 403;
}

server配置拓展

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
server
{
listen 80;
listen [::]:80;
#listen 443 ssl http2;
server_name demo.com demo2.com; #多个域名中间空格隔开

# 位置为 /etc/nginx/agent_deny.conf
include agent_deny.conf;

# 开ssl的话要开启证书验证
#ssl_certificate crt/pem; #ssl证书文件位置(常见证书文件格式为:crt/pem)
#ssl_certificate_key key; #ssl证书key位置
#ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
#ssl_session_timeout 60m;
#~~我觉得一般网站都不需要开启https,套个cf简简单单不好吗?~~
#使用证书真香

http{
...
map $HTTP_CF_CONNECTING_IP $real {
"" $remote_addr;
default $HTTP_CF_CONNECTING_IP;
}

limit_conn_zone $real zone=perserver:10m;
limit_conn perserver 20; # 每个IP 最多同时建立20个连接
limit_conn_log_level notice;
limit_req_zone $real zone=one:10m rate=15r/s; # 每个IP允许每秒发送10个请求
limit_req_log_level notice;
limit_req_status 403; # 返回状态403!
limit_req zone=one burst=10 nodelay; # 瞬时请求不超过 5 个,不延迟处理请求
...
}
server{
...
#也可以加到每个站点配置中
#limit_conn perserver 50;
#limit_req zone=one burst=10 nodelay; # 不建议在子项中开启nodelay,如果有一些静态文件特别多很容易触发流控,可以适当增加这两个值。
#limit_rate_after 10m; #前10m不限速
#limit_rate 512k; #流量限制
...
}

#auth_basic "Please input password"; #这里是验证时的提示信息
#auth_basic_user_file /home/htpasswd.pass;
#我们一般都会搭建比较隐私一点或者不想让其他人访问的页面,
#那么我们就可以加上密码验证,这个页面比较简陋不能够使用密码软件填充,就像tr的登录界面一样。
#这里添加密码验证则打开域名就会要求输入密码。

#关闭nginx版本号显示
vim /etc/nginx/nginx.conf
#在http段取消注释或者增加下面代码
server_tokens off;

#禁止其他域名解析到自己服务器,同时禁止直接使用IP访问网站,同时禁止IP泄露证书。
1.http:
server
{
listen 80 default_server;
server_name _;
return 500;
}

2.https:
server
{
listen 443 default_server ssl;
server_name _;
ssl_certificate 随便设置一个ssl证书;
ssl_certificate_key 随便设置一个ssl证书的key;
return 500;
}
3.http&https:
server
{
listen 80 default_server;
listen 443 default_server ssl;
server_name _;
ssl_certificate 随便设置一个ssl证书;
ssl_certificate_key 随便设置一个ssl证书的key;
return 500;
}

# 强制https,根据端口
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}

# 强制https,根据协议
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}


location /
{
root /www/wwwroot/blog/; # 打开就能看到index.html的文件夹路径
index index.html; # 主页
#auth_basic "password Required";
#auth_basic_user_file /home/htpasswd.pass;
# 密码生成往下看
# 这里添加密码验证则匹配时才会要求输入密码。
}

location /dl
{
alias /home/user/dl/; # 对于alias的dl后的"/"必须存在
charset utf-8,gbk; # 避免中文乱码
autoindex on; # 开启目录文件列表
autoindex_exact_size on; # 显示出文件的确切大小,单位是bytes
autoindex_localtime on; # 显示的文件时间为文件的服务器时间
}

location /nginx-test
{
echo $clientRealIp;
}
#当用户访问 /nginx-test 的时候,我们输出 $clientRealIp 变量,看看这个变量值是不是真的用户源IP地址

# 路径301 302重定向
rewrite ^/baidu(.*) https://www.baidu.com$1 permanent; # 301
rewrite ^/baidu(.*) https://www.baidu.com$1 redirect; # 302

# 域名301、302重定向
# rewrite 指令允许你使用正则表达式来匹配 URL,并将其重写为另一个 URL。可以进行复杂的重定向逻辑。
rewrite ^/(.*)$ https://shyi.org/$1 permanent; # 301
rewrite ^/(.*)$ https://shyi.org/$1 redirect; # 302

return 301 https://shyi.org$request_uri; # 301
return 302 https://shyi.org$request_uri; # 302


# 基于if的域名301 302重定向,使用if可能会出现一些问题,不建议使用if条件块
# if ($request_uri ~* "/old")
# if ($http_user_agent ~* "MSIE")
if ($host ~ '^demo.com') {
return 301 https://www.baidu.com$request_uri;
#return 302 https://www.baidu.com$request_uri;
}

# 使用 location 块进行重定向
location /old {
return 301 http://example.com/new;
}

# 日志可以不写
#access_log /www/wwwlogs/demo.com.log;
#error_log /www/wwwlogs/demo.com.error.log;
}

root和alias的区别:

root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径

alias是一个目录别名的定义,root则是最上层目录的定义。还有一个重要的区别是alias后面必须要用”/“结束,否则会找不到文件的,而root则可有可无。

1
2
3
4
5
6
7
8
	location /dl
{
#root /home/user/downloads; # 错误写法
alias /home/user/downloads/;
}
#这里如果是root访问 demo.com/dl 实际上nginx提供的是 /home/user/downloads/dl,所以会404
#如果是alias的话,访问 demo.com/dl 实际上nginx提供的是 /home/user/downloads/。

重定向

1
2
3
4
5
6
7
8
9
server
{
listen 80;
listen 443 ssl;
ssl_certificate crt/pem;
ssl_certificate_key key;
server_name shyi.io www.shyi.io;
rewrite ^/(.*)$ https://shyi.org/$1 permanent; #301
}
agent_deny

需要“include agent_deny.conf;”,放在server或者location范围内。位置为/etc/nginx/agent_deny.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 禁止Scrapy等工具的抓取
if ($http_user_agent ~* (Scrapy|Curl|HttpClient))
{
return 403;
}

# 禁止指定UA及UA为空的访问
if ($http_user_agent ~* "WinHttp|WebZIP|FetchURL|node-superagent|java/|FeedDemon|Jullo|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|Java|Feedly|Apache-HttpAsyncClient|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|BOT/0.1|YandexBot|FlightDeckReports|Linguee Bot|^$")
{
return 403;
}


# 禁止非GET|HEAD|POST方式的抓取
if ($request_method !~ ^(GET|HEAD|POST)$)
{
return 403;
}

# 禁止爬虫,if语句必须放在server或者location范围内,不能放在http范围内。
if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot" )
{
return 403;
}
获取访客真实IP
  • Remote Address:他是TCP中的概念,是无法伪造的,在应用程序中获取到的Remote Address值,是直接和应用服务器建立TCP连接的IP,可能是用户真实ip(用户直接访问应用服务器时),也可能是代理服务器(通过nginx负载均衡代理时)。
  • 在有CDN的情况下,remote_addr获取的是最后一个与你握手的ip(CDN的ip)。

一、1.在nginx.conf或者site.conf里的非server{}添加如下

1
2
3
4
5
log_format  main  '$http_x_forwarded_for- $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"';
例如
# log_format main '$real - $http_cf_ipcountry $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" "$remote_addr" "$request_time"';
#可以放在nginx.conf,也可以放在site.conf里,但是注意不要放入serve{}里
#这里的main可以自己更改,但是下面要一致。

2.添加main

1
access_log  /www/wwwlogs/test.log main;

二、1.需要在 http{}段 里添加(非server{})

1
2
3
4
5
#获取用户真实ip并赋值给$clientRealIP
#0-z能够完整获取到ipv6,而0-9只能获取ipv4
map $http_x_forwarded_for $clientRealIP{
"" $remote_addr;
~^(?P<firstAddr>[0-z\.]+),?.*$ $firstAddr; }

2.在nginx.conf或者site.conf里的非server{}添加如下

1
2
3
4
log_format  main  '$clientRealIP- $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"';
例如
# log_format main '$real - $http_cf_ipcountry $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" "$remote_addr" "$request_time"';
#可以放在nginx.conf,也可以放在site.conf里。

3.在access_log后面添加main

1
access_log  /www/wwwlogs/test.log main;
1
这样改变不了"$remote_addr"的值,只需要在用到 "$remote_addr"改成$clientRealIP即可

最后 nginx -s reload即可。详情查看后续文章。

有一些变量需要改一下,比如map中的 $real , $clientRealIP

log_format main 中的 $real , $clientRealIP , $http_x_forwarded_for

log_formatmap 要放在server{}以外

Nginx获取用户真实ip

搭建图床

nginx生成访问密码:

安装htpasswd工具:

1
2
3
4
5
(yum安装):yum -y install httpd-tools -y

(debian):apt-get install apache2-utils -y

(离线安装):rpm -ivh httpd-tools-2.4.6-88.el7.centos.x86_64.rpm

生成用户名和密码

1
2
3
4
5
6
7
8
htpasswd -bc /home/htpasswd.pass admin 123456
#其中/home/htpasswd.pass是生成密码文件路径
htpasswd -b /home/htpasswd.pass user 123456 # 多添加一个认证用户sea
htpasswd -b /home/htpasswd.pass user 1234567 # 修改密码
htpasswd -D /home/htpasswd.pass user # 删除用户名和密码
cat /home/htpasswd.pass
#显示admin:$apr1$G9CsNcuD$lctm0cOoDTx0h5CW7lKVc/
#其中admin是用户名,分号后面就是密码(密码已经加过密)
自签证书
1
2
3
4
5
6
7
8
9
10
11
12
13
14
openssl req \
-newkey rsa:2048 \
-x509 \
-nodes \
-keyout file.key \
-new \
-out file.crt \
-subj /CN=xxx.ccss.vip \
-reqexts SAN \
-extensions SAN \
-config <(cat /usr/local/openssl/openssl.cnf \
<(printf '[SAN]\nsubjectAltName=DNS:xxx.com,IP:11.11.11.11')) \
-sha256 \
-days 3650

引用:

部分参数参考自宝塔面板

Nginx 服务器安装及配置文件详解 | 菜鸟教程 (runoob.com)

Nginx 相关 | QuickBox Lite 知识库 (ptbox.dev)

搭建北邮人BT反代域名过程小记 - R酱小窝 ~ 个人博客 (rhilip.info)

使用nginx反代北邮人实现无IPV6环境访问北邮人 - carlo’ blogs (carloo.cc)

Nginx 反向代理 htpps 站点 502 排查思路 | 一小步 (smalloutcome.com)

nginx配置访问密码

centos nginx 卸载 - nickchou - 博客园 (cnblogs.com)

Nginx之location详解

文件路径 alias与root区别

Nginx重新编译添加模块_服务器应用_Linux公社-Linux系统门户网站 (linuxidc.com)

Nginx 限制单个IP的并发连接数/速度防止恶意攻击/蜘蛛爬虫采集

Nginx通过UserAgent屏蔽蜘蛛和采集

Nginx 挂CDN 如何获取真实访客IP地址

$_SERVER[“REMOTE_ADDR”] gives server IP rather than visitor IP

Google Public DNS IP addresses


Nginx基本配置
https://shyi.org/posts/16650/
作者
Shyi
发布于
2022年4月27日
更新于
2024年9月7日
许可协议