一文带你搞懂Nginx,老王出品,必属精品
前言Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器同时也提供了IMAP/POP3/SMTP服务一、nginx 基础常用命令1. ./nginx -t #检查配置文件的语法的正确性并尝试打开配置文件中所引用到的文件。 2. ./nginx -c /home/xx/nginx.conf #指定一个配置文件来代替缺省的。 3. ./nginx -v #nginx 的版本。 4. ./nginx -s reload #reload 会重新加载配置文件Nginx服务不会中断。而且reload时会测试conf语法等。 5. ./nginx #启动nginx。 6. ./nginx -s stop #stop 会立即停止服务这种方法比较强硬无论进程是否在工作都直接停止进程。 7. ./nginx -s quit #quit 较stop相比就比较温和一些了需要进程完成当前工作后再停止。nginx的Nginx 处理请求的路由机制# 块 A你的真实业务 server { listen 8080 ssl; server_name www.baidu.com; # ... 业务配置 } # 块 B兜底配置 server { listen 8080 ssl default_server; server_name _; return 403; } listen 8080 ssl default_server; 配置讲解Nginx 对 default_server 的校验非常严格对于任何一个特定的监听地址例如 0.0.0.0:15080只能有一个 server 块被标记为 default_server。 server_name _; 配置讲解在 Nginx 中下划线 _ 没有任何特殊的技术含义。它的作用纯粹是 “语义化” 和 “约定俗成”二、nginx 配置反向代理转发rewrite、proxy_pass# rewrite 关键字使用场景 rewrite regex replacement [flag]; regex 表示正则匹配规则。 replacement 表示跳转后的内容。 flag 表示 rewrite 支持的 flag 标记。 flag标记说明 last 本条规则匹配完成后不终止重写后的url匹配一般用在 server 和 if 中。 break 本条规则匹配完成即终止终止重写后的url匹配一般使用在 location 中。 redirect 返回302临时重定向浏览器地址会显示跳转后的URL地址。 permanent 返回301永久重定向浏览器地址栏会显示跳转后的URL地址。 # rewrite permanent (301永久重定向浏览器地址栏会显示跳转后的URL地址) 用法 # 请求url http://127.0.0.1:8080/abc/index.html location /abc { rewrite (.) http://www.yy.com/bbs$1 permanent; } # 通过rewrite permanent用户的浏览器地址栏会发生变化(301永久重定向) # 直接跳转到http://www.yy.com/bbs/abc/index.html # rewrite permanent (执行顺序 rewrite 永远先于 proxy_pass 执行) 用法 # 请求url http://127.0.0.1:8080/abc/index.html location /abc { # 浏览器会直接跳转到 yy.com请求到此为止 rewrite ^/abc/(.*)$ http://www.yy.com/bbs/$1 permanent; # 这行代码永远不会被执行 proxy_pass http://127.0.0.1:8080; } # 通过rewrite permanent重定向url地址为http://www.yy.com/bbs/index.html # rewrite break (本条规则匹配完成即终止终止重写后的url匹配) 用法 # 请求url http://127.0.0.1:8080/app_service/applog/index.html location ~ ^/app_service/(applog|watch) { rewrite ^/app_service/(.*)$ /chinaunicom/$1 break; proxy_pass http://127.0.0.1:19080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } # 通过 rewrite breakURI 被内部修改为 /chinaunicom/applog/index.html # proxy_pass 代理地址http://127.0.0.1:8080/chinaunicom/applog/index.html # rewrite last (本条规则匹配完成后不终止重写后的url匹配) 用法 # 请求url http://127.0.0.1:8080/abc/test location /abc { # 把 /abc/test 重写为 /bbs/test然后拿着 /bbs/test 重新去匹配 location rewrite ^/abc/(.*)$ /bbs/$1 last; # 这行代码会被跳过因为 Nginx 跑去重新匹配 location 了 proxy_pass http://127.0.0.1:8080; } # 如果请求变成了 /bbs/test就会匹配到这里 location /bbs { proxy_pass http://127.0.0.1:9090; # 最终执行的是这个代理 } # 通过rewrite last代理转发的url地址为http://127.0.0.1:9090/bbs/test #location 带/结尾 # 请求url http://127.0.0.1:8080/proxy/index.html location /proxy { rewrite ^/proxy/(.*)$ /$1 break; proxy_pass http://127.0.0.1:8080; } # 通过rewrite break代理转发的url地址为http://127.0.0.1:8080/index.html location /proxy/ { proxy_pass http://127.0.0.1:8080/; } # 代理地址以 / 结尾代理转发的url地址为http://127.0.0.1:8080/index.html location /proxy/ { proxy_pass http://127.0.0.1:8080; } # 代理地址不以 / 结尾代理转发的url地址为http://127.0.0.1:8080/proxy/index.html location /proxy/ { proxy_pass http://127.0.0.1:8080/tomcat/; } # 代理地址以 tomcat/ 结尾代理转发的url地址为http://127.0.0.1:8080/tomat/index.html location /proxy/ { proxy_pass http://127.0.0.1:8080/tomcat; } # 代理地址以 tomcat 代理转发的url地址为http://127.0.0.1:8080/proxytomcat/index.html #location 不带/结尾 location /proxy { proxy_pass http://127.0.0.1:8080/tomcat; } # 代理地址以 tomcat 代理转发的url地址为http://127.0.0.1:8080/tomcat/index.html location /proxy { proxy_pass http://127.0.0.1:8080/; } # 代理地址以 / 代理转发的url地址为http://127.0.0.1:8080//index.html location /proxy { proxy_pass http://127.0.0.1:8080; } # 代理地址不以 / 代理转发的url地址为http://127.0.0.1:8080/proxy/index.html #alias与root location / { alias /www/abc/; } # 当匹配路径为 / 时alias引用的路径结尾 “需要加 /” location /test/ { alias /www/abc/; } # 使用alias当访问/test/时会到/www/abc/目录下找文件 location / { root /www/abc; } # 当匹配路径为 / 时root引用的路径结尾 “不需要加 /” location /test/ { root /www/abc; } # 使用root当访问/test/时会到/www/abc/test/目录下找文件如果没有test目录会报403 #多层nginx代理Websocket服务 location /secure/socket { add_header backendIP $upstream_addr; add_header backendCode $upstream_status; proxy_redirect off; proxy_connect_timeout 6000; proxy_read_timeout 6000; proxy_send_timeout 6000; proxy_set_header Host 192.168.9.101:8087; proxy_pass http://localhost:8080/websocket/web; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection upgrade; proxy_set_header token $arg_username; } #多层nginx代理Java服务 #主机127.0.0.1下的nginx配置port8081 location /local/app/ { proxy_pass http://10.9.103.36:8081/; } #主机10.9.103.36下的nginx配置port8081 location /chat/ { proxy_pass http://10.186.253.117:8081/; } #多层nginx代理HTML资源 #主机10.9.103.35下的nginx配置port8083 location ^~ /html/chat/ { #符号^~一旦匹配到就不继续匹配(静态资源匹配) proxy_pass http://10.9.103.36:8081/; } #主机10.9.103.36下的nginx配置port8081(下图为html资源目录结构) location /mystatic { root html; index index.html index.htm; }//真实提供服务的为10.186.253.117:8081这个服务多层nginx代理Java服务Stringurlhttp://127.0.0.1:8081/local/app/chat/LargeModelLayout/session/sso;HashMapString,ObjectrequestBodynewHashMap(1);requestBody.put(app,kuandai);MapString,ObjectobjectMapHttpUtils.doJsonGet(url,newHashMap(0),requestBody);//Java多层代理分析//http://127.0.0.1:8081/local/app 匹配到第一层nginx代理转发到http://10.9.103.36:8081/;//http://10.9.103.36:8081/chat 匹配到第二层nginx代理转发到http://10.186.253.117:8081/;//最终格式url为http://10.186.253.117:8081/LargeModelLayout/session/sso//由http://10.186.253.117:8081/LargeModelLayout/session/sso提供服务//真实HTML资源由10.9.103.36:8083提供服务多层nginx代理HTML资源Stringurlhttp://10.9.103.35:8083/html/chat/mystatic/;//HTML多层代理分析//http://10.9.103.35:8083/html/chat/ 匹配到第一层nginx代理转发到http://10.9.103.36:8081///http://10.9.103.36:8081/mystatic/ 匹配到第二次nginx静态资源目录(html/mystatic)//最终格式url为http://10.9.103.36:8081/mystatic/index.html//请注意上文静态资源配置的注意事项三、nginx 配置负载均衡策略默认为轮询策略支持 轮循 Round Robin、加权轮循 Weighted Round Robin、最少连接数 Least Connection、源 IP 哈希 Source IP Hash等多种策略upstream webservers{ server 127.0.0.1:8080; server 127.0.0.1:8081; server 127.0.0.1:8082; } location / { #转发到负载服务上 proxy_pass http://webservers; }四、openssl生成证书1. openssl生成ssl证书1.1. 下载opens ssl并安装http://slproweb.com/products/Win32OpenSSL.html 官网地址1.2. 安装openssl且配置环境变量新增系统变量变量名 OPENSSL_HOME 变量值 D:\Program Files\OpenSSL-Win64\bin(以自己实际安装路径为准)1.3. 在path变量内新增内容 %OPENSSL_HOME%1.4. 在 ssl 文件夹下执行命令行操作1.4.1.创建私钥: openssl genrsa -des3 -out nj.key 10241.4.2.创建csr证书openssl req -new -key nj.key -out nj.csr1.4.3.复制文件 copy nj.key nj.key.copy1.4.4.去除密码 openssl rsa -in nj.key.copy -out nj.key1.4.4.生成 crt 证书 openssl x509 -req -days 365 -in nj.csr -signkey nj.key -out nj.crt五、nginx 配置ssl、 实现http转https1.1.nginx.conf配置文件#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main $remote_addr - $remote_user [$time_local] $request # $status $body_bytes_sent $http_referer # $http_user_agent $http_x_forwarded_for; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 443 ssl; server_name mt.hello.com; #ssl on; ssl_certificate ../ssl/nj.crt; ssl_certificate_key ../ssl/nj.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location /chat/ { proxy_pass http://jd.hello.com:8089; } } server { listen 80; #填写绑定证书的域名 server_name mt.hello.com; #强制将http的URL重写成https rewrite ^(.*) https://$server_name$1 permanent; } server { listen 8081; server_name mt.hello.com; location /chat/ { proxy_pass http://jd.hello.com:8089; } location /proxy/nginx/ { if ($host mt.hello.com) { rewrite ^(.*)$ http://jd.hello.com/$1 permanent; } proxy_pass http://jd.hello.com:8089/; } location /proxy/ { if ($host mt.hello.com) { rewrite ^(.*)$ http://www.baidu.com permanent; } proxy_pass http://jd.hello.com:8089/; } location /error/ { if ($host jd.hello.com) { return 404; } proxy_pass http://jd.hello.com:8089/; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location /50x.html{ root html; } location /404.html{ root html; } location /mystatic { root html; index index.html index.htm; } } }1.2.配置ssl证书443为https的默认端口、实现http协议转https协议六、nginx 配置rewrite实现新旧域名平滑更换、配置错误代码转发七、静态资源映射location/error/{if($hostjd.hello.com){return404;}proxy_pass http://jd.hello.com:8089/;}error_page404/404.html;error_page500502503504/50x.html;location/50x.html{root html;}location/404.html{root html;}#https://mt.hello.com/mystatic/index.html location/mystatic{root html;index index.html index.htm;}#https://mt.hello.com/request/request.html location/request{root html;index index.html index.htm;}#https://mt.hello.com/slider/src/images/Pic0.jpg location/slider{root html;index index.html index.htm;}七、nginx快速安装教程(nginx-1.22.1-版本)rpm-qa|grep gcc yum-y install gcc zlib-devel openssl-devel pcre-devel groupadd nginx useradd-r-g nginx-s/sbin/nologin nginx cd nginx-1.22.1/./configure--usernginx--groupnginx--prefix/usr/local/nginx--with-http_ssl_module makemake install lscpu|grep ^CPU(s)|awk {print $2} cd/usr/local/nginxvim conf/nginx.conf server_tokens off;ln-s/usr/local/nginx/sbin/nginx/usr/sbin/nginx-Vnginx-s reload curl-I127.0.0.1

相关新闻

开发第一线:新Windows与开发环境:大模型LLM/量化 Win-Mac统一快捷键体验 win强制关机:shutdown /s /f /t 0

开发第一线:新Windows与开发环境:大模型LLM/量化 Win-Mac统一快捷键体验 win强制关机:shutdown /s /f /t 0

对于开发者用Windows来说,最大的建议就是不要用Windows,既然必须要用,那就来吧!!! win强制关机:shutdown /s /f /t 0 一、必装软件 1.搜狗/百度输入法,-> 双拼 2.Chrome浏览器 3.Everything (墙裂推荐!!) 4.wsl + git for windows 5.anaconda3 + vs c…

2026/10/10 8:12:42 阅读更多 →
基于PCA9422与STM32的便携设备电源管理方案设计

基于PCA9422与STM32的便携设备电源管理方案设计

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/10 8:11:42 阅读更多 →
SAC-Auto路径规划实战:激光雷达避障仿真中的强化学习调参与工程落地

SAC-Auto路径规划实战:激光雷达避障仿真中的强化学习调参与工程落地

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/10 8:11:41 阅读更多 →

最新新闻

代码随想录67天刷题总结:算法模板、避坑与面试转化

代码随想录67天刷题总结:算法模板、避坑与面试转化

代码随想录刷到第67天,说实话,这一天比我想象中来得平静。没有“终于结束了”的解脱感,也没有“我全都学会了”的兴奋,更多的是一种踏实的收束感。从第一天的数组二分查找开始,到后来二叉树、回溯、动规、单调栈&#…

2026/10/11 13:10:49 阅读更多 →
探索地块建立全解析:Java+JS+Python三端协作实战

探索地块建立全解析:Java+JS+Python三端协作实战

从赛题公布到最终提交,我前后花了将近两周时间。“新卷200分”里的这道“探索地块建立”,要求用三种语言各完成一轮闭环,确实不是单纯考某个语法点能应付过去的。很多朋友一看到“探索地块建立(Java & JS & Python&#x…

2026/10/11 13:10:49 阅读更多 →
Cursor 智能提交实战:用 AI 生成规范 Git Commit Message 的完整工作流

Cursor 智能提交实战:用 AI 生成规范 Git Commit Message 的完整工作流

最近我的 git 提交流程发生了不小的变化。以前写完代码顺手敲一句“fix bug”“update code”“改了一堆东西”就推了,等过了两周回来看历史记录,完全想不起来当时改了啥。后来我开始试着让 Cursor 的 AI 帮我生成 commit message,再进一步让…

2026/10/11 13:10:49 阅读更多 →
微信小程序实时语音识别接入指南:从鉴权到帧流处理

微信小程序实时语音识别接入指南:从鉴权到帧流处理

简介:微信小程序语音识别项目是一套面向微信小程序开发者的完整工程示例,围绕科大讯飞语音识别接口展示语音转文字、实时语音输入与智能语音交互的实现思路,适合具备一定JavaScript基础、希望在小程序中快速接入AI语音能力的开发者学习。压缩…

2026/10/11 13:10:49 阅读更多 →
基于SSM的软件缺陷管理系统:从选题到答辩全流程详解

基于SSM的软件缺陷管理系统:从选题到答辩全流程详解

每到毕业季,群里最热闹的问题永远是“毕设做什么题目好”。作为一个经常带学生做项目的过来人,我的回答一般都很直接:软件缺陷管理系统,这个题目别嫌弃它老,放到2026年依然是性价比极高的选择。只要有SSM框架和Java基础…

2026/10/11 13:10:49 阅读更多 →
如何看懂 Portabase 安全机制:AES-256-GCM凭据加密、RBAC与Passkey登录完整指南

如何看懂 Portabase 安全机制:AES-256-GCM凭据加密、RBAC与Passkey登录完整指南

【免费下载链接】portabase Portabase - Database backup & restore tool for PostgreSQL, MySQL, MsSQL, MariaDB, Firebird SQL, SQLite, MongoDB, Redis and Docker Volume 项目地址: https://gitcode.com/gh_mirrors/por/portabase 点击查看 免费下载 Por…

2026/10/11 13:09:49 阅读更多 →

日新闻

流感时间序列预测实战:ARIMA/LSTM全流程拆解与避坑指南

流感时间序列预测实战:ARIMA/LSTM全流程拆解与避坑指南

简介:基于 ARIMA、LSTM、Transformer 等模型的流感时间序列预测 Python 源码,面向计算机相关专业课程设计与期末大作业学生,以及项目实战学习者。内容覆盖预处理、平稳性检验、定阶、残差分析、多模型对比预测的完整时序建模流程,…

2026/10/11 0:00:27 阅读更多 →
影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别

影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别

影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别 做影刀RPA自动化,十个新手有八个栽在"往输入框里填东西"这件事上:要么填不进去,要么填了一半,要么直接把原来内容追加在后面。这背后的根因&…

2026/10/11 0:00:27 阅读更多 →
影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容

影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容

影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容 1. 认识影刀:什么场景该用RPA采小说数据 起点中文网的页面结构相对稳定——分类榜单、书籍详情、章节内容三块独立页面,跳转链路清晰。这种场景非常适合影刀自动化&#x…

2026/10/11 0:00:27 阅读更多 →

周新闻

流感时间序列预测实战:ARIMA/LSTM全流程拆解与避坑指南

流感时间序列预测实战:ARIMA/LSTM全流程拆解与避坑指南

简介:基于 ARIMA、LSTM、Transformer 等模型的流感时间序列预测 Python 源码,面向计算机相关专业课程设计与期末大作业学生,以及项目实战学习者。内容覆盖预处理、平稳性检验、定阶、残差分析、多模型对比预测的完整时序建模流程,…

2026/10/11 0:00:27 阅读更多 →
影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别

影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别

影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别 做影刀RPA自动化,十个新手有八个栽在"往输入框里填东西"这件事上:要么填不进去,要么填了一半,要么直接把原来内容追加在后面。这背后的根因&…

2026/10/11 0:00:27 阅读更多 →
影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容

影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容

影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容 1. 认识影刀:什么场景该用RPA采小说数据 起点中文网的页面结构相对稳定——分类榜单、书籍详情、章节内容三块独立页面,跳转链路清晰。这种场景非常适合影刀自动化&#x…

2026/10/11 0:00:27 阅读更多 →

月新闻

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/11 10:45:37 阅读更多 →
Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/9 21:32:20 阅读更多 →
黑夜航拍船只数据集训练YOLOV5模型全流程解析

黑夜航拍船只数据集训练YOLOV5模型全流程解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/10 10:38:42 阅读更多 →