【檀越剑指大厂—Nginx】Nginx篇
- 游戏开发
- 2025-08-21 05:51:02

一.安装 #yum 安装ningx #nginx添加yum repro库中 # 下载nginx包 wget 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 二.命令 #nginx常用操作 #常用操作是基于nginx正确安装的基础上的操作 #启动nginx systemctl start nginx #停止 nginx systemctl stop nginx #重启 nginx systemctl restart nginx #重新加载配置 systemctl reload nginx #设置开机启动 systemctl enable nginx #关闭开机启动设置 systemctl disable nginx 三.配置 1.配置目录 /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 { } } 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
【檀越剑指大厂—Nginx】Nginx篇由讯客互联游戏开发栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“【檀越剑指大厂—Nginx】Nginx篇”