nginx的配置文件位于/etc/nginx下,其中nginx.conf 是主配置文件
conf.d包含其他的配置文件,虚拟主机配置一般放在该目录下,配置文件以conf结尾
/etc/nginx/
├── conf.d
│ ├── default.conf
│ └── test.conf
├── fastcgi_params
├── mime.types
├── modules -> ../../usr/lib64/nginx/modules
├── nginx.conf
├── scgi_params
└── uwsgi_params
配置结构
存在于 main 上下文中的配置指令如下:
user
worker_processes
error_log
events
http
mail
存在于 http 上下文中的指令如下:
server
存在于 mail 上下文中的指令如下:
server
auth_http
pop3_capabilities
imap_capabilities
存在于 server 上下文中的配置指令如下:
listen
server_name
access_log
location
protocol
proxy
smtp_auth
xclient
存在于 location 上下文中的指令如下:
index
alias
root
一般root写在location中,但也可以写在server下
location / 里面的root会覆盖外面的server下的root配置
location 其他的配置root会追加在root根目录的后面
想在location中将请求的文件指定到非root目录下去找可以使用alias
location ^~ /test {
alias /data/www/location;
}
http://test.testnginx.org/test/index.html
请求会找/data/www/location/index.html,将location的路径直接替换成alias的
root和alias的区别
- root:该指令用于定义请求的根目录。Nginx会将请求的URI附加到
root
指令指定的路径上,以构建最终的文件系统路径。root
指令可以在server
或location
指令中使用。 - alias:该指令也用于定义请求的根目录,但它不会将请求的URI附加到指定的路径上。相反,它使用指定的路径替换URI中的位置部分。
alias
指令只能在location
指令中使用,并且其后通常需要跟一个以斜杠(/)结尾的路径。
location模块
正则须知
常用的正则表达式
^ :匹配输入字符串的起始位置
$ :匹配输入字符串的结束位置
* :匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll”
+ :匹配前面的字符一次或多次。如“ol+”能匹配“ol”及“oll”、“olll”,但不能匹配“o”
? :匹配前面的字符零次或一次,例如“do(es)?”能匹配“do”或者“does”,”?”等效于”{0,1}”
. :匹配除“\n”之外的任何单个字符,若要匹配包括“\n”在内的任意字符,请使用诸如“[.\n]”之类的模式
\ :将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n”匹配一个换行符,而“\$”则匹配“$”
\d :匹配纯数字[0-9] \s :空白符 \w :任意单词字符包括下划线[A-Za-z0-9_]
{n} :重复 n 次
{n,} :重复 n 次或更多次
{n,m} :重复 n 到 m 次
[] :定义匹配的字符范围
[c] :匹配单个字符 c
[a-z] :匹配 a-z 小写字母的任意一个
[a-zA-Z0-9] :匹配所有大小写字母或数字
() :表达式的开始和结束位置
| :或运算符
匹配分为三种
- 精准匹配:location = / {…}
- 一般匹配:location / {…}
- 正则匹配:location ~ / {…}
location 常用的匹配规则:
= :进行普通字符精确匹配,也就是完全匹配。
^~ :表示普通字符匹配。使用前缀匹配,不匹配正则表达。
~ :区分大小写的匹配。
~* :不区分大小写的匹配。
!~ :区分大小写的匹配取非。
!~* :不区分大小写的匹配取非。
@ 用于内部跳转的命名location
合并连续的/符号 Merge_slashes on
Location匹配规则的优先级
优先级:具体文件路径 > 精准配置 = > 前缀匹配 ^~ > 正则匹配 ~,~,!~,!~ > 一般前缀匹配 /XXXX > 通用匹配 /
首先匹配具体文件路径例如/a/index.html,找不到文件的精确匹配,在找精确匹配,在没有精准匹配的时候,先看所有前缀的长度,取最长匹配的location;
如果最长的前缀匹配是带有^~的则不看其它的正则匹配,直接使用^~的location匹配用户的访问路径并跳转页面,然后匹配正则表达式。
虽然正则理论上优先级高于一般前缀匹配,但是匹配的顺序是先匹配一般前缀,再去匹配正则
正则匹配看上下顺序,根据配置文件的配置由上往下依次匹配,匹配到即停止
location / {
root /data/www/test;
index index.html index.htm;
}
location ^~ /test {
alias /data/www/location;
index index.html index.htm;
}
location /test/index.html {
return 200 "111";
}
location =/test/index.html {
return 200 "222";
}
以上配置, =/test/index.html 》 /test/index.html 》 ^~ /test 》 /
@内部重定向
#error_page 404 http://www.baidu.com # 直接这样是不允许的
error_page 404 = @fallback;
location @fallback {
proxy_pass http://www.baidu.com;
}
rewrite URL重写
rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标记位实现URL重写以及重定向。
比如:
- 更换域名后需要保持旧的域名能跳转到新的域名上
- 某网页发生改变需要跳转到新的页面
- 网站防盗链等等需求
rewrite只能放在server{},location{},if{}中,并且默认只能对域名后边的除去传递的参数外的字符串起作用
rewrite基于语句
rewrite regex replacement flag
rewirte 旧地址 新地址【选项】
选项:
- last :本条规则匹配完成后,不终止重写后的url匹配,一般用在 server 和 if 中。
- break :本条规则匹配完成即终止,终止重写后的url匹配,一般使用在 location 中。
- redirect :返回302临时重定向,浏览器地址会显示跳转后的URL地址。
- permanent :返回301永久重定向,浏览器地址栏会显示跳转后的URL地址。百度等抓取时不会抓永久重定向过去的地址
rewrite /a.html /b.html;注:不加选择项,那么url不会跳转,只有内容的跳转
rewrite a.html /b.html;注:同上,新地址必须加/路径
rewrite a.html /b.html redirect;
rewrite a.html /b.html permanent;
将旧的域名永久重定向到新域名用到$host变量
permanent表示永久重定向
if ($host = "blog.shadowwu.club"){
rewrite /(.*)$ http://www.shadowwu.club/$1 permanent;
if判断重定向
if ($uri ~ ^/abc/123.html){
rewrite ^/ /;
}
^/表示匹配整个url 后面的重写一般与源url无关
$request_uri
:包含请求参数的原始URI,不包含主机名,如:http://www.kgc.com/abc/bbs/index.html?a=1&b=2 中的 /abc/bbs/index.php?a=1&b=2$uri
:这个变量指当前的请求URI,不包括任何参数,如:/abc/bbs/index.html$document_uri
:与$uri相同,这个变量指当前的请求URI,不包括任何传递参数,如:/abc/bbs/index.html$query_string
:请求参数Parameter
if条件判断
1、直接判断变量
if ($slow) {}
如果变量值是空字符串或者”0″时,判定为false;否则,判定为true。
1.0.1版本前,任何以”0″开头的字符串,判定为false。
2、字符串相等
使用 “=” 和 “!=” 操作符判断是否等于字符串
if ($request_method = POST) {}
3、正则匹配
~:判断时区分字符大小写
~:判断时不区分字符大小写
!~:判断时区分字符大小写
!~:判断时不区分字符大小写
4、文件及目录判断
“-f”, “!-f”:判断指定的路径是否为存在且为文件;
“-d”, “!-d”:判断指定的路径是否为存在且为目录;
“-e”, “!-e”:判断指定的路径是否存在,文件或目录均可;
“-x”, “!-x”:判断指定路径的文件是否存在且可执行;
if (!-f “$document_root/index.html”) {}
正向代理
普通http正向代理
server
{
resolver 8.8.8.8;
listen 82;
location /
{
proxy_pass http://$http_host$request_uri;
}
}
正向代理https需要ngx_http_proxy_connect_module模块,不是官方的,需要自己打补丁和编译添加
正向代理隐藏客户端,反向代理隐藏服务端
nginx实现代理上网,有三个关键点必须注意,其余的配置跟普通的nginx一样
1.增加dns解析resolver
2.增加无server_name名的server
3.proxy_pass指令
resolver表示DNS服务器
location表示匹配用户访问的资源,并作进一步转交和处理,可用正则表达式匹配
proxy_pass表示需要代理的地址
$http_host 表示用户访问资源的主机部分
$request_uri 表示用户访问资源的URI部分。
server {
##代理端口
listen 3100;
resolver 192.168.125.21 114.114.114.114 valid=300s;
resolver_timeout 10s;
#server_name localhost;
proxy_connect;
proxy_connect_allow 443 80;
proxy_connect_connect_timeout 10s;
proxy_connect_read_timeout 10s;
proxy_connect_send_timeout 10s;
location / {
proxy_set_header Host $host;
proxy_pass $scheme://$http_host$request_uri;
proxy_buffers 256 4k;
proxy_max_temp_file_size 0k;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_next_upstream error timeout invalid_header http_502;
}
# 启用日志记录
access_log /usr/local/nginx/logs/proxy.log;
}
使用代理服务器访问的服务器日志
192.168.125.20 – – [05/May/2024:10:27:16 +0800] “GET / HTTP/1.0” 304 – “-” “Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:125.0) Gecko/20100101 Firefox/125.0”
[root@centos70 ~]# curl -I -v --proxy 192.168.125.20:3100 http://nginx.org
* About to connect() to proxy 192.168.125.20 port 3100 (#0)
* Trying 192.168.125.20...
* Connected to 192.168.125.20 (192.168.125.20) port 3100 (#0)
> HEAD http://nginx.org/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: nginx.org
> Accept: */*
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Server: nginx/1.24.0
Server: nginx/1.24.0
< Date: Sun, 05 May 2024 02:29:40 GMT
Date: Sun, 05 May 2024 02:29:40 GMT
< Content-Type: text/html; charset=utf-8
Content-Type: text/html; charset=utf-8
< Content-Length: 7127
Content-Length: 7127
< Connection: keep-alive
Connection: keep-alive
< Last-Modified: Tue, 23 Apr 2024 22:50:34 GMT
Last-Modified: Tue, 23 Apr 2024 22:50:34 GMT
< ETag: "66283b3a-1bd7"
ETag: "66283b3a-1bd7"
< Accept-Ranges: bytes
Accept-Ranges: bytes
非代理访问的服务器日志
192.168.125.1 - - [05/May/2024:10:27:23 +0800] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0"
[root@centos70 ~]# curl -I -v http://nginx.org
* About to connect() to nginx.org port 80 (#0)
* Trying 52.58.199.22...
* Connected to nginx.org (52.58.199.22) port 80 (#0)
> HEAD / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: nginx.org
> Accept: */*
>
< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Server: nginx/1.25.3
Server: nginx/1.25.3
< Date: Sun, 05 May 2024 02:30:58 GMT
Date: Sun, 05 May 2024 02:30:58 GMT
< Content-Type: text/html; charset=utf-8
Content-Type: text/html; charset=utf-8
< Content-Length: 7127
Content-Length: 7127
< Last-Modified: Tue, 23 Apr 2024 22:50:34 GMT
Last-Modified: Tue, 23 Apr 2024 22:50:34 GMT
< Connection: keep-alive
Connection: keep-alive
< Keep-Alive: timeout=15
Keep-Alive: timeout=15
< ETag: "66283b3a-1bd7"
ETag: "66283b3a-1bd7"
< Accept-Ranges: bytes
Accept-Ranges: bytes
<
* Connection #0 to host nginx.org left intact
反向代理
upstream 设置轮询,集群
语法
upstream webserver{
#ip_hash
server ip地址/或者ip+端口 参数1=xx 参数2=xx 参数3=xxx 参数4=xxx
server ip地址/或者ip+端口 参数1=xx 参数2=xx 参数3=xxx 参数4=xxx
}
ip_hash 分配固定ip
默认轮询,不固定ip,轮回访问多个server
参数:
1、weight可以设置后台服务器的权重值,默认为1
2、max_fails可以设置后台服务器的最大连接失败次数
3、fail_timeout可以设置后台服务器的失败超时时间,单位为秒
4、down标记服务器已关机,不参与集群调度
注意upstream在http模块下,server模块上,放错位置报错
nginx: [emerg] “upstream” directive is not allowed here in /etc/nginx/conf.d/test.conf:9
upstream webserver {
#ip_hash;
server 192.168.125.20 weight=2 max_fails=1 fail_timeout=30;
server 192.168.125.21 weight=1 max_fails=1 fail_timeout=30;
server 127.0.0.0:81 down;
}
server{
listen 80;
server_name testnginx.org
test.testnginx.org
www.testnginx.org;
root /data/www/test;
location / {
index index.php index.html index.htm;
proxy_pass http://webserver; #通过反向代理,调用集群
rewrite a.html /b.html permanent;
}
}
stream ssl代理
需要编译时安装模块–with-stream ,stream模块时最顶层模块和http模块同级
cat > /usr/local/nginx/conf/mynginx.conf << EOF
stream {
upstream sshserver {
server 192.168.125.21:22;
}
server {
listen 220;
proxy_pass sshserver;
}
}
EOF
echo “include /usr/local/nginx/conf/mynginx.conf;” >> /etc/nginx/nginx.conf
[root@centos70 ~]# ssh 192.168.125.20 -p 220
The authenticity of host '[192.168.125.20]:220 ([192.168.125.20]:220)' can't be established.
ECDSA key fingerprint is SHA256:Gxcj6RO87QeIIlFRRqQDMBve41KvAleP1tOjlyQD/rI.
ECDSA key fingerprint is MD5:a2:ae:1a:8b:b0:28:c9:1d:ca:14:6a:05:d7:80:90:69.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or 'no': yes
Warning: Permanently added '[192.168.125.20]:220' (ECDSA) to the list of known hosts.
root@192.168.125.20's password:
Last login: Sun May 5 09:17:47 2024 from 172.28.16.1
[root@centos71 ~]#
通过以上配置可以看到我们通过192.168.125.20的220端口对192.168.125.21进行了ssh访问,host由centos70 变成了centos71
打开资源限制和并发测试
在Linux中,ulimit命令用于限制用户对shell资源的访问,包括进程数、文件打开数等。这些限制可以分为软限制(soft limit)和硬限制(hard limit)。
软限制(soft limit)是当前系统生效的设置值,可以理解为一种警告的设定。当资源使用超过这个限制时,系统并不会立即阻止,而是会发出警告信息,提示用户已经接近或超过了限制。用户可以在软限制范围内继续使用资源,但应当注意避免超过硬限制。
硬限制(hard limit)是系统中所能设定的最大值,用户不能超过这个限制。只有当程序进程具有root权限时,才有可能突破硬限制。硬限制是确保系统稳定性和安全性的重要手段,可以防止用户过度消耗系统资源导致系统崩溃或其他问题。
总的来说,软限制和硬限制的主要区别在于它们的严格程度和可调整性。软限制更为宽松,允许用户在一定范围内超过限制,而硬限制则更为严格,必须严格遵守。同时,它们也共同构成了Linux系统中对用户资源使用的管理和控制机制。
[root@centos71 ~]# ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 63966
max locked memory (kbytes, -l) 65536
max memory size (kbytes, -m) unlimited
open files (-n) 1048576
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) unlimited
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
临时修改
[root@centos71 ~]# ulimit -n 65535
[root@centos71 ~]# ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 63966
max locked memory (kbytes, -l) 65536
max memory size (kbytes, -m) unlimited
open files (-n) 65535
永久修改打开文件 /etc/security/limits.conf
* hard nofile 65535
* soft nofile 60000
*表示任意用户,hard硬限制,soft软限制,nofile打开文件数量 重启生效
修改nginx配置
worker_processes 1; #同cpu核心数相同,启动工作的进程数量
#查看cpu核心数,top 然后输入1
events {
worker_connections 60000; #进程连接数量限制
}
http{
client_header_buffer_size 1k; #默认的请求包缓存大小
large_client_header_buffers 4 4k; #最大请求包缓存个数与容量
}
自定义报错页面和支持中文
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
charset UTF-8;
error_page 404 /404.html;
}
cat /usr/share/nginx/html/404.html
<h1>你来到了知识的荒漠,请寻找正确的旅途</h1>
查看服务器状态
编译添加
–with-http_stub_status_module
location /status {
stub_status on;
allow 192.168.125.20;
deny all;
}
压缩
http{
gzip on; #开启压缩
gzip_min_length 1k; #1k以内字节的页面不压缩
gzip_comp_level 3; #压缩比例1~9等级
gzip_types text/xml image/png text/plain application/json; #压缩类型到 /etc/nginx/mime.types查看
}
nginx日志
access.log的相关配置
我们可以在$nginx_home/conf/nginx.conf配置文件中对nginx请求日志进行配置。配置的格式为:
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
其中,access_log是关键字,表示接下来的配置是关于access日志的配置,path为该日志文件的存储路径,后面还可以对日志输出格式、是否压缩、日志刷新时间等设置进行配置。可能有读者会留意到,上面截图中的用例在path后面写上了main,其实这里的main并不是什么关键字,而是nginx默认定义好的一个日志格式名称,我们可以在log_format中看到,nginx默认定义了一个名为main的日志输出格式。
access_log配置在http模块下
关闭access日志
access_log off;
默认变量格式:
log_format combined '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
$remote_addr变量:记录了客户端的IP地址(普通情况下)。
$remote_user变量:当nginx开启了用户认证功能后,此变量记录了客户端使用了哪个用户进行了认证。
$time_local变量:记录了当前日志条目的时间。
$request变量:记录了当前http请求的方法、url和http协议版本。
$status变量:记录了当前http请求的响应状态,即响应的状态码,比如200、404等响应码,都记录在此变量中。
$body_bytes_sent变量:记录了nginx响应客户端请求时,发送到客户端的字节数,不包含响应头的大小。
$http_referer变量:记录了当前请求是从哪个页面过来的,比如你点了A页面中的超链接才产生了这个请求,那么此变量中就记录了A页面的url。
$http_user_agent变量:记录了客户端的软件信息,比如,浏览器的名称和版本号。
增加变量:
"$http_host" "$request_time" "$upstream_response_time" "$upstream_connect_time" "$upstream_header_time"';
$http_host 请求地址,即浏览器中你输入的地址(IP或域名)
$request_time:处理请求的总时间,包含了用户数据接收时间
$upstream_response_time:建立连接和从上游服务器接收响应主体的最后一个字节之间的时间
$upstream_connect_time:花费在与上游服务器建立连接上的时间
$upstream_header_time:建立连接和从上游服务器接收响应头的第一个字节之间的时间
错误日志
错误日志位于http外
错误日志定义的格式:
error_log path(日志存放路径)level(日志等级[debug| info | notice | warn | error | crit])
日志界别的程度:从左到右,日志详细程度逐渐减少,dbug是最详细的,严重级别也是最轻的
debug主要用于调试代码用等,一般严重级别为crit和error,遇到crit和error必须立马解决,warn有空的时候解决,影响不大的话可以不用理会。
扩展:
error日志不能通过以下方法关闭
error_log off;
这个表示你的错误日志是写入off文件中的
正确关闭错误日志的方法应该如下
error_log /dev/nul;相当于把错误日志放入垃圾桶里面
Nginx防盗链
让自己网站的视频和图面不被爬虫抓取或者被复制,这就是防盗链。
location ~ \.(jpg|png|gif)$ {
valid_referers none blocked 192.168.125.20;
if ($invalid_referer){
return 404;
}
}
[root@eab5aec2df6c ~]# curl --referer http://192.168.125.21 -I http://test.testnginx.org/html/test.jpg
HTTP/1.1 404 Not Found
Server: nginx/1.26.0
Date: Sun, 05 May 2024 07:33:07 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 64
Connection: keep-alive
ETag: "66370044-40"
[root@eab5aec2df6c ~]# curl --referer http://192.168.125.20 -I http://test.testnginx.org/html/test.jpg
HTTP/1.1 200 OK
Server: nginx/1.26.0
Date: Sun, 05 May 2024 07:33:14 GMT
Content-Type: image/jpeg
Content-Length: 880814
Last-Modified: Sun, 05 May 2024 07:16:25 GMT
Connection: keep-alive
ETag: "66373249-d70ae"
Accept-Ranges: bytes
curl中 –referer这个选项是指定我们从哪里跳转过来
-I 只显示http response的头部信息
valie_referer指定的语法:
valie_referer none blocked server_names string.....:
valie_referer是用来获取referer头部域中的值,并且根据该值的情况给nginx全局变量
valie_referer的值,如果referer头域中没有符合valid referers指令的值,$valie_referer变量将会被复制为1
- none检测referer头域不存在的请求
- bocked检测referer头域中值被防火墙或者代理服务器删除或伪装的情况
- server_names设置一个或者多个url
Nginx SSL配置
部署HTTPS加密网站,加密模块-with-http_ssl_module
加密算法一般分为:对称加密算法、非对称加密算法、信息摘要
对称加密算法:AES、DES主要用于单机数据加密
非对称加密算法:RSA、DSA主要用于网路数据加密
信息摘要:MD5、sha256、sha512主要用于对数据的完整校验
在Nginx上配置SSL证书的过程大致如下:
1、准备SSL证书和私钥。可以从信任的证书颁发机构购买或免费获取。确保将证书(通常为.crt或.pem格式)和私钥(通常为.key格式)保存在安全的位置。
2、在Nginx配置文件中添加SSL配置。这通常涉及编辑或创建nginx.conf文件。在server块中添加以下指令:
listen 443 ssl;:指定Nginx在443端口上监听HTTPS连接。
ssl_certificate /path/to/certificate.crt;:指定证书文件的路径。
ssl_certificate_key /path/to/private.key;:指定私钥文件的路径。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;:指定支持的SSL协议版本。
ssl_ciphers 'CipherSuite';:指定加密算法套件,以增强安全性。
ssl_session_cache shared:SSL:10m;:设置SSL会话缓存的大小和时间。
ssl_session_timeout 5m;:设置SSL会话的超时时间。
3、重新加载Nginx配置。使用命令如sudo nginx -s reload来使新的配置生效。
验证配置。使用nginx -t命令来测试配置文件的正确性。
4、确保HTTP请求重定向到HTTPS。在server块中添加重定向规则,将所有HTTP请求自动重定向到HTTPS,例如,使用return 301 https://$host$request_uri;语句。
配置HTTPS严格传输安全(HSTS)。通过添加add_header Strict-Transport-Security “max-age=31536000; includeSubDomains”;到配置中,可以提高安全性。
参考
Nginx的location匹配和rewrite重写 https://blog.csdn.net/liu_xueyin/article/details/134931244
Nginx之location 匹配规则详解 https://blog.csdn.net/thlzjfefe/article/details/99308437
nginx if 指令 https://blog.csdn.net/sayyy/article/details/136338148
nginx正向代理、反向代理 https://www.cnblogs.com/dgp-zjz/p/13654970.html
ulimit.conf中soft和hard区别及常用配置 https://www.cnblogs.com/ydswin/p/18049375