nginx的使用
一、安装
centos7中yum安装
#安装yum源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
#查看所有版本
yum list nginx --showduplicates
#安装最新版本
yum -y install nginx
#安装指定版本
yum -y install nginx-1.18.0-2.el7.ngx
#启动
systemctl start nginx
源码安装
# 1.创建一个文件夹,存放安装的nginx文件
mkdir /nginx
# 2.安装nginx依赖包
yum -y install gcc pcre-devel zlib-devel
# 3.解压下载好的nginx源码包
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
# 4.配置编译并安装
./configure --prefix=/nginx
make
make install
# 5.启动nginx
/nginx/sbin/nginx
# 6.设置开机自启
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/nginx/sbin/nginx
ExecReload=/nginx/sbin/nginx -s restart
ExecStop=/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# 7.关闭nginx设置开机自启
systemctl enable nginx.service
systemctl start nginx
二、配置文件
/usr/share/nginx/html #网站文件默认存放目录
/etc/nginx/conf.d/default.conf #默认站点配置
/etc/nginx/conf.d/ #自定义nginx站点配置文件目录
/etc/nginx/nginx.conf #全局配置
初始化安全配置
listen 443 ssl;
server_name anc.xxx.cn;
ssl_certificate cert/anc.xxx.pem;
ssl_certificate_key cert/anc.xxx.key;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5:!3DES; #强安全加密
#ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #弱安全加密
ssl_protocols TLSv1.2 TLSv1.3; #安全连接
ssl_prefer_server_ciphers on;
server_tokens off; #关闭版本号
add_header X-Frame-Options "SAMEORIGIN"; #控制页面是否可以在<frame>、<iframe>中展示,只能在相同来源的页面上被嵌入展示
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"; #只能通过https,使用前需要充分的测试
三、日志配置
四、参数定义
1、nginx代理文件夹,开启目录流量,类似FTP
# server 或location 段里配置
#启用目录流量,相当于ftp
autoindex on;
#默认为on,显示出文件的确切大小,单位是bytes。
#改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
autoindex_exact_size off;
#默认为off,显示的文件时间为GMT时间。
#改为on后,显示的文件时间为文件的服务器时间
autoindex_localtime on;
2、参数配置
try_files $uri $uri/ /index.html; #当uri页面访问不存在时,自动重定向到首页
proxy_set_header Host $http_host; #获取客户端主机名,发送到代理服务器
proxy_set_header X-Real-IP $remote_addr;#获取客户端真实IP,发送到代理服务器
proxy_set_header REMOTE-HOST $remote_addr;#自定义的HTTP头字段,将客户端的IP地址添加到HTTP请求头中,以便目标服务器可以获取到这个信息
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #代理服务器添加的 X-Forwarded-For 字段的值,将代理服务器添加的 X-Forwarded-For 字段的值添加到新的请求头中,以便目标服务器可以获取到这个信息。
3、正则表达式
^ :匹配输入字符串的起始位置
$ :匹配输入字符串的结束位置
* :匹配前面的字符零次或多次。如“ol*”能匹配“o”及“ol”、“oll”
+ :匹配前面的字符一次或多次。如“ol+”能匹配“ol”及“oll”、“olll”,但不能匹配“o”
? :匹配前面的字符零次或一次,例如“do(es)?”能匹配“do”或者“does”,”?”等效于”{0,1}”
. :匹配除“\n”之外的任何单个字符,若要匹配包括“\n”在内的任意字符,请使用诸如“[.\n]”之类的模式
\ :将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用。如“\n”匹配一个换行符,而“\$”则匹配“$”
\d :匹配纯数字
\w :匹配字母或数字或下划线或汉字
\s :匹配任意的空白符
\b :匹配单词的开始或结束
{n} :重复 n 次
{n,} :重复 n 次或更多次
{n,m} :重复 n 到 m 次
[] :定义匹配的字符范围
[c] :匹配单个字符 c
[a-z] :匹配 a-z 小写字母的任意一个
[a-zA-Z0-9] :匹配所有大小写字母或数字
() :表达式的开始和结束位置
| :或运算符
#常用的表达式
= :进行普通字符精确匹配,也就是完全匹配。
^~ :表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其它 location。
~ :区分大小写的匹配。
~* :不区分大小写的匹配。
!~ :区分大小写的匹配取非。
!~* :不区分大小写的匹配取非。
#匹配优先级
首先精确匹配 =
其次前缀匹配 ^~
再其次是按文件中顺序的正则匹配 ~或~*
然后匹配不带任何修饰的前缀匹配(一般匹配)
最后是交给 / 通用匹配
示例
(1)location = / {}
=为精确匹配 / ,主机名后面不能带任何字符串,比如访问 / 和 /data,则 / 匹配,/data 不匹配
再比如 location = /abc,则只匹配/abc ,/abc/或 /abcd不匹配。若 location /abc,则即匹配/abc 、/abcd/ 同时也匹配 /abc/。
(2)location / {}
因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 比如访问 / 和 /data, 则 / 匹配, /data 也匹配,
但若后面是正则表达式会和最长字符串优先匹配(最长匹配)
(3)location /documents/ {}
匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它 location后面的正则表达式没有匹配到时,才会采用这一条
(4)location /documents/abc {}
匹配任何以 /documents/abc 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它 location后面的正则表达式没有匹配到时,才会采用这一条
(5)location ^~ /images/ {}
匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条
(6)location ~* \.(gif|jpg|jpeg)$ {}
匹配所有以 gif、jpg或jpeg 结尾的请求
然而,所有请求 /images/ 下的图片会被 location ^~ /images/ 处理,因为 ^~ 的优先级更高,所以到达不了这一条正则
(7)location /images/abc {}
最长字符匹配到 /images/abc,优先级最低,继续往下搜索其它 location,会发现 ^~ 和 ~ 存在
(8)location ~ /images/abc {}
匹配以/images/abc 开头的,优先级次之,只有去掉 location ^~ /images/ 才会采用这一条
(9)location /images/abc/1.html {}
匹配/images/abc/1.html 文件,如果和正则 ~ /images/abc/1.html 相比,正则优先级更高
4、全局变量
remote_addr #客户端ip,如:192.168.4.2
binary_remote_addr #客户端ip(二进制)
remote_port #客户端port,如:50472
remote_user #已经经过Auth Basic Module验证的用户名
host #请求主机头字段,否则为服务器名称,如:dwz.stamhe.com
request #用户请求信息,如:GET /?_a=index&_m=show&count=10 HTTP/1.1
request_filename #当前请求的文件的路径名,由root或alias和URI request组合而成,如:/webserver/htdocs/dwz/index.php
status #请求的响应状态码,如:200
body_bytes_sent #响应时送出的body字节数数量。即使连接中断,这个数据也是精确的,如:40
content_length #请求头中的Content-length字段
content_type #请求头中的Content-Type字段
http_referer #引用地址
http_user_agent #客户端agent信息,如:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11
args #如:_a=index&_m=show&count=10
document_uri #与$uri相同,如:/index.php
document_root #针对当前请求的根路径设置值,如:/webserver/htdocs/dwz
hostname #如:centos53.localdomain
http_cookie #客户端cookie信息
cookie_COOKIE #cookie COOKIE变量的值
is_args #如果有$args参数,这个变量等于”?”,否则等于”",空值,如?
limit_rate #这个变量可以限制连接速率,0表示不限速
query_string #与$args相同,如:_a=index&_m=show&count=10
realpath_root #如:/webserver/htdocs/dwz
request_body #记录POST过来的数据信息
request_body_file #客户端请求主体信息的临时文件名
request_method #客户端请求的动作,通常为GET或POST,如:GET
request_uri #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。不能修改。如:/index.php?_a=index&_m=show&count=10
scheme #HTTP方法(如http,https),如:http
uri #如:/index.php
request_completion #如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty),如:OK
server_protocol #请求使用的协议,通常是HTTP/1.0或HTTP/1.1,如:HTTP/1.1
server_addr #服务器地址,在完成一次系统调用后可以确定这个值,如:192.168.4.129
server_name #服务器名称,如:dwz.stamhe.com
server_port #请求到达服务器的端口号,如:80
五、启用相关模块
1、启动echo模块(对调试比较有用)
2、使用健康检查模块
(1)nginx自带健康检查(被动检查)
server x.x.x.x:8080 max_fails=3 fail_timeout=30s;
server x.x.x.x:8080 max_fails=3 fail_timeout=30s;
#说明:代表在30秒内某一应用失败3次,认为该应用宕机,后等待30秒,这期间内不会再把新请求发送到宕机应用,而是直接发到正常的那一台,
#等待的这30秒时间到后再有请求进来继续尝试连接宕机应用且仅尝试1次,如果还是失败,则继续等待30秒...以此循环,直到恢复。`
(2)nginx主动check方法 ,nginx_upstream_check_module模块对后端节点做主动健康检查(淘宝开发)
#TCP协议主动探活
upstream str1 {
server ip:port;
server ip:port;
check interval=5000 rise=2 fall=3 timeout=1000 type=tcp;
}
#http协议主动探活
upstream str2 {
server ip:port;
server ip:port;
check interval=5000 rise=2 fall=3 timeout=1000 type=http;
check_http_send "HEAD /url HTTP/1.0\r\n\r\n"; # /url指提供健康检查的url,请求体不宜过大
check_http_expect_alive http_2xx http_3xx;
}
#也支持udp协议探测,比较少用,不做探究
server {
listen 80;
server_name localhost;
location /a/ {
proxy_pass http://str1;
}
location /b/ {
proxy_pass http://str2;
}
#check的web页面,可以看到服务存活列表
location /check {
check_status html;
access_log off;
}
}
浏览器访问check页面
安装方法
# 1、查看nginx版本
nginx version: nginx/1.24.0
# 2、下载nginx对应版本的源码
# 3、下载插件
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
编译配置,加上nginx已有的环境配置,最后在加上健康检查模块
然后执行 make 编译,不需要执行make install
然后生成 objs文件下的nginx可执行文件
#安装依赖环境
yum -y install gcc pcre-devel zlib-devel
#编译配置(根据自己的nginx环境进行配置)
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/home/nginx/nginx_upstream_check_module-master
#编译生成可执行文件
make
#备份原来的可执行文件
mv /usr/sbin/nginx /usr/sbin/nginx_back.bak
#替换可执行文件
cp objs/nginx /usr/sbin/nginx
六、ubuntu22.04LTS安装nginx
#添加Nginx官方的APT源:
sudo tee /etc/apt/sources.list.d/nginx.list << EOF
deb http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx
deb-src http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx
EOF
#添加Nginx的公钥:
wget http://nginx.org/keys/nginx_signing.key
sudo apt-key add nginx_signing.key
#更新APT包索引
sudo apt update
#安装Nginx:
sudo apt install nginx
#以上命令会安装Nginx的最新稳定版本。如果你想安装主线版本(可能不如稳定版稳定),你可以将上述步骤中的mainline替换为main。
#搜索
apt search nginx
#查看所有版本
apt list -a nginx
#安装指定版本
sudo apt install nginx=[version]
sudo apt install nginx=1.18.0-0ubuntu1
七、麒麟v10(arm)安装nginx
#安装
rpm -Uvh --nodeps http://nginx.org/packages/centos/8/aarch64/RPMS/nginx-1.26.2-1.el8.ngx.aarch64.rpm
rpm -Uvh --nodeps http://nginx.org/packages/centos/8/aarch64/RPMS/nginx-debuginfo-1.26.2-1.el8.ngx.aarch64.rpm
rpm -Uvh --nodeps http://nginx.org/packages/centos/8/aarch64/RPMS/nginx-module-image-filter-1.26.2-2.el8.ngx.aarch64.rpm
rpm -Uvh --nodeps http://nginx.org/packages/centos/8/aarch64/RPMS/nginx-module-image-filter-debuginfo-1.26.2-2.el8.ngx.aarch64.rpm
rpm -Uvh --nodeps http://nginx.org/packages/centos/8/aarch64/RPMS/nginx-module-njs-1.26.2%2B0.8.6-1.el8.ngx.aarch64.rpm
rpm -Uvh --nodeps http://nginx.org/packages/centos/8/aarch64/RPMS/nginx-module-otel-1.26.2%2B0.1.0-1.el8.ngx.aarch64.rpm
rpm -Uvh --nodeps http://nginx.org/packages/centos/8/aarch64/RPMS/nginx-module-otel-debuginfo-1.26.2%2B0.1.0-1.el8.ngx.aarch64.rpm
rpm -Uvh --nodeps http://nginx.org/packages/centos/8/aarch64/RPMS/nginx-module-otel-debuginfo-1.26.2%2B0.1.0-1.el8.ngx.aarch64.rpm
rpm -Uvh --nodeps http://nginx.org/packages/centos/8/aarch64/RPMS/nginx-module-perl-debuginfo-1.26.2-2.el8.ngx.aarch64.rpm
rpm -Uvh --nodeps http://nginx.org/packages/centos/8/aarch64/RPMS/nginx-module-xslt-1.26.2-2.el8.ngx.aarch64.rpm
rpm -Uvh --nodeps http://nginx.org/packages/centos/8/aarch64/RPMS/nginx-module-xslt-debuginfo-1.26.2-2.el8.ngx.aarch64.rpm
#卸载
rpm -e --nodeps nginx-module-perl-debuginfo-1.26.2-2.el8.ngx.aarch64
rpm -e --nodeps nginx-debuginfo-1.26.2-1.el8.ngx.aarch64
rpm -e --nodeps nginx-module-njs-1.26.2+0.8.6-1.el8.ngx.aarch64
rpm -e --nodeps nginx-module-otel-1.26.2+0.1.0-1.el8.ngx.aarch64
rpm -e --nodeps nginx-module-xslt-1.26.2-2.el8.ngx.aarch64
rpm -e --nodeps nginx-module-image-filter-1.26.2-2.el8.ngx.aarch64
rpm -e --nodeps nginx-module-xslt-debuginfo-1.26.2-2.el8.ngx.aarch64
rpm -e --nodeps nginx-module-image-filter-debuginfo-1.26.2-2.el8.ngx.aarch64
rpm -e --nodeps nginx-module-otel-debuginfo-1.26.2+0.1.0-1.el8.ngx.aarch64
rpm -e --nodeps nginx-1.26.2-1.el8.ngx.aarch64
#删除相关文件
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?