一.安装
1.功能
- 静态代理
- 负载均衡
- 黑白名单
- 限流
- 缓存
- 反向代理
2.安装步骤
#yum 安装ningx
#nginx添加yum repro库中
# 下载nginx包
wget https://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 建立nginx的yum仓库
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
#查看nginx的信息
yum info nginx
#查看yum源仓库中nginx版本
yum --showduplicates list nginx | expand
#安装nginx,默认安装最新的稳定版本 及 nginx 1.20.2
yum install nginx
#查看版本
nginx -V
二.命令
1.启动 nginx
#启动nginx
systemctl start nginx
2.停止 nginx
#停止 nginx
systemctl stop nginx
3.重启 nginx
#重启 nginx
systemctl restart nginx
4.重新加载配置
#重新加载配置
systemctl reload nginx
5.设置开机启动
#设置开机启动
systemctl enable nginx
6.关闭开机启动设置
#关闭开机启动设置
systemctl disable nginx
7.检查配置是否正确
#检查配置是否正确(常用)
nginx -t
8.重新加载
#进入nginx可执行目录sbin下,输入命令,不用重启
./nginx -s reload
三.配置
1.配置目录
# 从 / 根目录下查找文件名为 nginx.conf 的文件
find / -name nginx.conf
# 从 /etc 目录下查找文件名为 nginx.conf 的文件
find /etc -name nginx.conf
nginx -t
/etc/nginx
2.执行目录
/usr/sbin/nginx
3.站点目录
/usr/share/nginx/html
4.我的 nginx 配置
server {
listen 80;
server_name qinyingjie.top;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost:8080/;
}
location ~ .*\.(gif|jpg|jpeg|png|jfif)$ {
root /kwan/;
autoindex on ;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
-
=
:用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求 -
~
:用于表示 uri 包含正则表达式,并且区分大小写 -
~*
:用于表示 uri 包含正则表达式,并且不区分大小写 -
^~
:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求。字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。
5.服务器
120.79.36.53 qinyingjie.top
四.特性
1.域名
upstream qinyingjie.top {
server localhost:8080;
}
location / {
proxy_pass http://qinyingjie.top;
}
2.图片不展示
location ~ .*\.(gif|jpg|jpeg|png|jfif)$ {
proxy_pass http://qinyingjie.top;
root /kwan/;
autoindex on;
}
3.负载均衡
1.轮训
upstream open-api-test-category{
server 10.250.16.111:8195;
server 10.250.16.111:8196;
}
location / {
proxy_pass http://open-api-test-category/;
}
2.权重
upstream open-api-test-category{
server 10.250.16.111:8195 weight=1;
server 10.250.16.111:8196 weight=3;
}
location / {
proxy_pass http://open-api-test-category/;
}
3.hash
upstream open-api-test-category{
server 10.250.16.111:8195;
server 10.250.16.111:8196;
ip_hash;
}
location / {
proxy_pass http://open-api-test-category/;
}
4.动静分离
location ~ .*\.(gif|jpg|jpeg|png|jfif)$ {
root /kwan/ ;
autoindex on ;
}
5.域名
server_name qinyingjie.top;
6.反向代理
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost:8080/;
}
7.root 和 alias 区别
最基本的区别:alias 指定的目录是准确的,root 是指定目录的上级目录,并且该上级目录要含有 location 指定名称的同名目录。另外,根据前文所述,使用 alias 标签的目录块中不能使用 rewrite 的 break。
- alias 虚拟目录配置中,location 匹配的 path 目录如果后面不带"/“,那么访问的 url 地址中这个 path 目录后面加不加”/“不影响访问,访问时它会自动加上”/“;
- 但是如果 location 匹配的 path 目录后面加上”/“,那么访问的 url 地址中这个 path 目录必须要加上”/“,访问时它不会自动加上”/“。如果不加上”/“,访问就会失败!
- root 目录配置中,location 匹配的 path 目录后面带不带”/",都不会影响访问。
在 nginx 配置中的良好习惯是:
- 在 location /中配置 root 目录;
- 在 location /path 中配置 alias 虚拟目录。
#直观理解如下形式:
location /dev/{
alias /web/dev/doc/; #这个查找文件的路径直接是/web/dev/doc/
}
location /dev/{
root /web/dev/doc/; #这个查找文件的路径应该是/web/dev/doc/dev
}
# 这里使用root配置 如果访问 192.168.2.3/pak/a.html 则对应的路径为:/usr/local/pak/a.html
# 通过root配置则location配置的/pak/一定是要在root对应的/usr/local/目录下要有的目录
8.proxy_pass
结论:
- 如果 proxy_pass 末尾有斜杠/,proxy_pass 不拼接 location 的路径
- 如果 proxy_pass 末尾无斜杠/,proxy_pass 会拼接 location 的路径
8.1.proxy_pass 末尾有斜杠
location /api/ {
proxy_pass http://127.0.0.1:8000/;
}
请求地址:http://localhost/api/test
转发地址:http://127.0.0.1:8000/test
8.2.proxy_pass 末尾无斜杠
location /api/ {
proxy_pass http://127.0.0.1:8000;
}
请求地址:http://localhost/api/test
转发地址:http://127.0.0.1:8000/api/test
8.3.proxy_pass 包含路径,且末尾有斜杠
location /api/ {
proxy_pass http://127.0.0.1:8000/user/;
}
请求地址:http://localhost/api/test
转发地址:http://127.0.0.1:8000/user/test
8.4.proxy_pass 包含路径,末尾无斜杠
location /api/ {
proxy_pass http://127.0.0.1:8000/user;
}
请求地址:http://localhost/api/test
转发地址:http://127.0.0.1:8000/usertest
五.docker 安装 nginx
#查找nginx
docker search nginx
#下载镜像
docker pull nginx:latest
#查看本地镜像
docker images
#运行容器
docker run --name nginx-test -p 8080:80 -d nginx
参数说明:
--name nginx-test:容器名称。
-p 8080:80: 端口进行映射,将本地 8080 端口映射到容器内部的 80 端口。
-d nginx: 设置容器在在后台一直运行。
#访问是否成功
127.0.01:8080