1 环境
| 系统或软件 | 版本 |
|---|---|
| CentOS | 7.9 |
| Nginx | nginx-1.22.0.tar.gz |
2 前置依赖
| 软件包 | 链接 |
|---|---|
gcc、gcc-c++
|
gcc、gcc-c++ |
pcre、pcre-devel
|
pcre |
zlib、zlib-devel
|
zlib |
openssl、openssl-devel
|
openssl |
# 安装gcc和gcc-c++
sudo yum -y install gcc gcc-c++
# 安装pcre和pcre-devel
sudo yum -y install pcre pcre-devel
# 安装zlib和zlib-devel
sudo yum -y install zlib zlib-devel
# 安装openssl和openssl-devel
sudo yum -y install openssl openssl-devel
3 编译安装
3.1 下载源码包
Nginx下载地址,选用stable version(稳定版本)
3.2 下载和解压源码包
# 新建目录
mkdir -p /opt/src
cd /opt/src
# 下载源码
wget http://nginx.org/download/nginx-1.22.0.tar.gz
# 解压到/opt目录
tar -zxvf nginx-1.22.0.tar.gz -C /opt
# 进入解压后的目录
cd /opt/nginx-1.22.0/
3.3 源码安装
编译配置
./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
执行安装命令
make && make install
添加软链让nginx命令可以全局执行
ln -s /opt/nginx/sbin/nginx /usr/local/bin/nginx
验证Nginx的版本
nginx -v
4 Nginx常用命令
| 命令 | 描述 |
|---|---|
nginx 或 nginx -c $dir/nginx.conf
|
启动 |
nginx -t |
配置检查 |
nginx -s stop |
停止 |
nginx -s reload |
热加载配置 |
5 添加到系统服务
添加新系统服务文件
vim /lib/systemd/system/nginx.service
脚本内容
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/opt/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
脚本授权
chmod 755 /lib/systemd/system/nginx.service
命令
| 命令 | 功能 |
|---|---|
systemctl start nginx.service |
启动Nginx |
systemctl stop nginx.service |
停止Nginx |
systemctl restart nginx.service |
重启Nginx |
systemctl status nginx.service |
查看Nginx状态 |
systemctl enable nginx.service |
设置Nginx开机自动启动 |
systemctl disable nginx.service |
禁用Nginx开机自动启动 |
安装完
Nginx后就可以根据需要对每个单独的域名新建一个${HOST_NAME}.conf进行配置,配置完成之后调用nginx -s reload刷新配置,刷新前使用nginx -t验证配置文件的正确性。
6 Nginx 配置
6.1 root
指定静态资源目录位置,它可以写在 http 、 server 、 location 等配置中。
location /image {
root /opt/nginx/static;
}
当用户访问www.test.com/image/1.png时,实际在服务器找的路径是 /opt/nginx/static/image/1.png
6.2 location
location [ = | ~ | ~* | ^~ ] uri {
...
}
匹配规则:
-
=精确匹配; -
~正则匹配,区分大小写; -
~*正则匹配,不区分大小写; -
^~匹配到即停止搜索;
匹配优先级: = > ^~ > ~ > ~* > 不带任何字符。
示例:
server {
listen 80;
server_name www.test.com;
# 只有当访问 www.test.com/webris/ 时才会匹配到/usr/share/nginx/html/webris/index.html
location = /webris/ {
root /usr/share/nginx/html
index index.html
}
# 当访问 www.test.com/1.jpg 等路径时会去 /usr/share/nginx/images/1.jpg 找对应的资源
location ~ \.(jpeg|jpg|png|svg)$ {
root /usr/share/nginx/images;
}
# 当访问 www.test.com/bbs/ 时会匹配上 /usr/share/nginx/html/bbs/index.html
location ^~ /bbs/ {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
location 中的反斜线
不带 / 当访问 www.nginx-test.com/test 时, Nginx 先找是否有 test 目录,如果有则找 test 目录下的 index.html ;如果没有 test 目录, nginx 则会找是否有 test 文件。
location /test {
...
}
带 / 当访问 www.nginx-test.com/test 时, Nginx 先找是否有 test 目录,如果有则找 test 目录下的 index.html ,如果没有它也不会去找是否存在 test 文件。
location /test/ {
...
}
6.3 return
停止处理请求,直接返回响应码或重定向到其他 URL ;
执行 return 指令后, location 中后续指令将不会被执行。
示例:
location / {
return 404; # 直接返回状态码
}
location / {
return 404 "pages not found"; # 返回状态码 + 一段文本
}
location / {
return 302 /bbs ; # 返回状态码 + 重定向地址
}
location / {
return https://www.baidu.com ; # 返回重定向地址
}
6.4 rewrite
根据指定正则表达式匹配规则,重写 URL 。
6.5 proxy_pass
用于配置代理服务器。
语法:
proxy_pass URL;
示例:
proxy_pass http://127.0.0.1:8081
proxy_pass http://127.0.0.1:8081/proxy
URL 参数原则
-
URL必须以http或https开头; -
URL中可以携带变量; -
URL中是否带URI,会直接影响发往上游请求的URL;
两种常见的 URL 用法:
-
proxy_pass http://127.0.0.1:8080不带
/表示Nginx不会修改用户URL,而是直接透传给上游的应用服务器;
location /webris/{
proxy_pass http://127.0.0.1:8080;
}
分析:
- 用户请求
URL:/webris/abc/test.html - 请求到达
Nginx的URL:/webris/abc/test.html - 请求到达上游应用服务器的
URL:/webris/abc/test.html
-
proxy_pass http://127.0.0.1:8080/
带/表示Nginx会修改用户URL,修改方法是将location后的URL从用户URL中删除;
location /webris/{
proxy_pass http://127.0.0.1:8080/;
}
分析:
- 用户请求
URL:/webris/abc/test.html - 请求到达
Nginx的URL:/webris/abc/test.html - 请求到达上游应用服务器的
URL:/abc/test.html