diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..abe8b76 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,3 @@ +ko_fi: jaywcjlove +buy_me_a_coffee: jaywcjlove +custom: ["https://www.paypal.me/kennyiseeyou", "https://jaywcjlove.github.io/#/sponsor"] diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..e136fdb --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,64 @@ +name: CI +on: + push: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20 + registry-url: 'https://registry.npmjs.org' + + - run: npm install + - run: npm run build + + - name: Generate Contributors Images + uses: jaywcjlove/github-action-contributors@main + with: + filter-author: (renovate\[bot\]|renovate-bot|dependabot\[bot\]) + output: dist/CONTRIBUTORS.svg + avatarSize: 42 + + - name: Create Tag + id: create_tag + uses: jaywcjlove/create-tag-action@main + with: + package-path: ./package.json + + - name: Deploy Website + uses: peaceiris/actions-gh-pages@v4 + with: + user_name: 'github-actions[bot]' + user_email: 'github-actions[bot]@users.noreply.github.com' + commit_message: ${{ github.event.head_commit.message }} + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./dist + + - name: Generate Changelog + id: changelog + uses: jaywcjlove/changelog-generator@main + with: + token: ${{ secrets.GITHUB_TOKEN }} + filter-author: (jaywcjlove|小弟调调™|dependabot\[bot\]|Renovate Bot) + filter: (^[\s]+?[R|r]elease)|(^[R|r]elease) + + - name: Create Release + uses: ncipollo/release-action@v1 + if: steps.create_tag.outputs.successful + with: + allowUpdates: true + token: ${{ secrets.GITHUB_TOKEN }} + name: ${{ steps.create_tag.outputs.version }} + tag: ${{ steps.create_tag.outputs.version }} + body: | + [![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor) + + Documentation ${{ steps.changelog.outputs.tag }}: https://raw.githack.com/jaywcjlove/vim-web/${{ steps.changelog.outputs.gh-pages-short-hash }}/index.html + Comparing Changes: ${{ steps.changelog.outputs.compareurl }} + + ${{ steps.changelog.outputs.changelog }} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a093f12 --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +lib +dist +node_modules +coverage +test/out + +npm-debug.log* +lerna-debug.log +yarn-error.log +package-lock.json + +.DS_Store +.cache +.vscode +.idea + +*.bak +*.tem +*.temp +#.swp +*.*~ +~*.* + +# IDEA +*.iml +*.ipr +*.iws +.idea/ \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..64e05d5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 小弟调调™ + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 988a597..95fe32c 100755 --- a/README.md +++ b/README.md @@ -1,88 +1,95 @@ -

- + +

+ + +

Nginx 入门学习笔记

+ -Nginx 是一款面向性能设计的 HTTP 服务器,能反向代理 HTTP,HTTPS 和邮件相关(SMTP,POP3,IMAP)的协议链接。并且提供了负载均衡以及 HTTP 缓存。它的设计充分使用异步事件模型,削减上下文调度的开销,提高服务器并发能力。采用了模块化设计,提供了丰富模块的第三方模块。 +[![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor) -所以关于 Nginx,有这些标签:「异步」「事件」「模块化」「高性能」「高并发」「反向代理」「负载均衡」 +[Nginx](https://nginx.org/en/) 是一款面向性能设计的 HTTP 服务器,能反向代理 HTTP、HTTPS 和邮件相关(SMTP、POP3、IMAP)的协议链接,并且提供了负载均衡以及 HTTP 缓存。它的设计充分使用异步事件模型,削减上下文调度的开销,提高服务器并发能力。采用了模块化设计,提供了丰富的第三方模块。 -Linux系统:`Centos 7 x64` +关于 Nginx,有这些标签:「异步」「事件」「模块化」「高性能」「高并发」「反向代理」「负载均衡」 + +Linux系统:`Centos 7 x64` Nginx版本:`1.11.5` -目录 -=== - - - -- [安装](#安装) - - [安装依赖](#安装依赖) - - [下载](#下载) - - [编译安装](#编译安装) - - [nginx测试](#nginx测试) - - [设置全局nginx命令](#设置全局nginx命令) -- [Mac 安装](#mac-安装) - - [安装nginx](#安装nginx) - - [启动服务](#启动服务) -- [开机自启动](#开机自启动) -- [运维](#运维) - - [服务管理](#服务管理) - - [重启服务防火墙报错解决](#重启服务防火墙报错解决) -- [nginx卸载](#nginx卸载) -- [参数说明](#参数说明) -- [配置](#配置) - - [常用正则](#常用正则) - - [全局变量](#全局变量) - - [符号参考](#符号参考) - - [配置文件](#配置文件) - - [内置预定义变量](#内置预定义变量) - - [反向代理](#反向代理) - - [负载均衡](#负载均衡) - - [RR](#rr) - - [权重](#权重) - - [ip_hash](#ip_hash) - - [fair](#fair) - - [url_hash](#url_hash) - - [屏蔽ip](#屏蔽ip) -- [第三方模块安装方法](#第三方模块安装方法) -- [重定向](#重定向) - - [重定向整个网站](#重定向整个网站) - - [重定向单页](#重定向单页) - - [重定向整个子路径](#重定向整个子路径) -- [性能](#性能) - - [内容缓存](#内容缓存) - - [Gzip压缩](#gzip压缩) - - [打开文件缓存](#打开文件缓存) - - [SSL缓存](#ssl缓存) - - [上游Keepalive](#上游keepalive) - - [监控](#监控) -- [常见使用场景](#常见使用场景) - - [跨域问题](#跨域问题) - - [跳转到带www的域上面](#跳转到带www的域上面) - - [代理转发](#代理转发) - - [代理转发连接替换](#代理转发连接替换) - - [ssl配置](#ssl配置) - - [强制将http重定向到https](#强制将http重定向到https) - - [两个虚拟主机](#两个虚拟主机) - - [虚拟主机标准配置](#虚拟主机标准配置) - - [爬虫过滤](#爬虫过滤) - - [防盗链](#防盗链) - - [虚拟目录配置](#虚拟目录配置) - - [防盗图配置](#防盗图配置) - - [屏蔽.git等文件](#屏蔽git等文件) - - [域名路径加不加需要都能正常访问](#域名路径加不加需要都能正常访问) -- [错误问题](#错误问题) + + +## 目录 + +- [安装](#安装) + - [安装依赖](#安装依赖) + - [下载](#下载) + - [编译安装](#编译安装) + - [Nginx 测试](#nginx-测试) + - [设置全局 nginx 命令](#设置全局-nginx-命令) +- [Mac 安装](#mac-安装) + - [安装nginx](#安装nginx) + - [启动服务](#启动服务) +- [开机自启动](#开机自启动) +- [运维](#运维) + - [服务管理](#服务管理) + - [重启服务防火墙报错解决](#重启服务防火墙报错解决) +- [Nginx 卸载](#nginx-卸载) +- [参数说明](#参数说明) +- [配置](#配置) + - [常用正则](#常用正则) + - [全局变量](#全局变量) + - [符号参考](#符号参考) + - [配置文件](#配置文件) + - [内置预定义变量](#内置预定义变量) + - [反向代理](#反向代理) + - [负载均衡](#负载均衡) + - [RR](#rr) + - [权重](#权重) + - [ip_hash](#ip_hash) + - [fair](#fair) + - [url_hash](#url_hash) + - [屏蔽ip](#屏蔽ip) +- [第三方模块安装方法](#第三方模块安装方法) +- [重定向](#重定向) + - [重定向整个网站](#重定向整个网站) + - [重定向单页](#重定向单页) + - [重定向整个子路径](#重定向整个子路径) +- [性能](#性能) + - [内容缓存](#内容缓存) + - [Gzip压缩](#gzip压缩) + - [打开文件缓存](#打开文件缓存) + - [SSL缓存](#ssl缓存) + - [上游Keepalive](#上游keepalive) + - [监控](#监控) +- [常见使用场景](#常见使用场景) + - [跨域问题](#跨域问题) + - [跳转到带www的域上面](#跳转到带www的域上面) + - [代理转发](#代理转发) + - [监控状态信息](#监控状态信息) + - [代理转发连接替换](#代理转发连接替换) + - [SSL 配置](#ssl-配置) + - [强制将http重定向到https](#强制将http重定向到https) + - [两个虚拟主机](#两个虚拟主机) + - [虚拟主机标准配置](#虚拟主机标准配置) + - [爬虫过滤](#爬虫过滤) + - [防盗链](#防盗链) + - [虚拟目录配置](#虚拟目录配置) + - [防盗图配置](#防盗图配置) + - [屏蔽.git等文件](#屏蔽git等文件) + - [域名路径加不加需要都能正常访问](#域名路径加不加需要都能正常访问) + - [cockpit](#cockpit) +- [错误问题](#错误问题) - [精品文章参考](#精品文章参考) - + ## 安装 ### 安装依赖 -> prce(重定向支持)和openssl(https支持,如果不需要https可以不安装。) +> prce(重定向支持)和 openssl(https 支持,如果不需要 https 可以不安装)。 ```bash yum install -y pcre-devel @@ -90,18 +97,17 @@ yum -y install gcc make gcc-c++ wget yum -y install openssl openssl-devel ``` -CentOS 6.5 我安装的时候是选择的“基本服务器”,默认这两个包都没安装全,所以这两个都运行安装即可。 +在 CentOS 6.5 上安装时,我选择了“基本服务器”,默认情况下这两个包都没有安装全,所以需要运行上述命令进行安装。 ### 下载 -[nginx的所有版本在这里](http://nginx.org/download/) +[nginx 的所有版本在这里](http://nginx.org/download/) ```bash wget http://nginx.org/download/nginx-1.13.3.tar.gz wget http://nginx.org/download/nginx-1.13.7.tar.gz -# 如果没有安装wget -# 下载已编译版本 +# 如果没有安装 wget $ yum install wget # 解压压缩包 @@ -146,7 +152,7 @@ make make install ``` -### nginx测试 +### Nginx 测试 运行下面命令会出现两个结果,一般情况nginx会安装在`/usr/local/nginx`目录中 @@ -158,7 +164,7 @@ cd /usr/local/nginx/sbin/ # nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful ``` -### 设置全局nginx命令 +### 设置全局 Nginx 命令 ```bash vi ~/.bash_profile @@ -307,13 +313,13 @@ Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service t ```bash systemctl is-enabled servicename.service # 查询服务是否开机启动 -systemctl enable *.service # 开机运行服务 +systemctl enable *.service # 开机运行服务 systemctl disable *.service # 取消开机运行 -systemctl start *.service # 启动服务 -systemctl stop *.service # 停止服务 +systemctl start *.service # 启动服务 +systemctl stop *.service # 停止服务 systemctl restart *.service # 重启服务 -systemctl reload *.service # 重新加载服务配置文件 -systemctl status *.service # 查询服务运行状态 +systemctl reload *.service # 重新加载服务配置文件 +systemctl status *.service # 查询服务运行状态 systemctl --failed # 显示启动失败的服务 ``` @@ -344,16 +350,12 @@ chmod +x /etc/rc.d/rc.local ```bash # 启动 /usr/local/nginx/sbin/nginx - # 重启 /usr/local/nginx/sbin/nginx -s reload - # 关闭进程 /usr/local/nginx/sbin/nginx -s stop - # 平滑关闭nginx /usr/local/nginx/sbin/nginx -s quit - # 查看nginx的安装状态, /usr/local/nginx/sbin/nginx -V ``` @@ -411,7 +413,7 @@ service iptables restart ``` -## nginx卸载 +## Nginx 卸载 如果通过yum安装,使用下面命令安装。 @@ -531,12 +533,14 @@ yum remove nginx 例如请求:`http://localhost:3000/test1/test2/test.php` +``` $host:localhost $server_port:3000 $request_uri:/test1/test2/test.php $document_uri:/test1/test2/test.php $document_root:/var/www/html $request_filename:/var/www/html/test1/test2/test.php +``` ### 符号参考 @@ -1087,7 +1091,7 @@ server { } ``` -### 跳转到带www的域上面 +### 跳转到带 www 的域上面 ```nginx server { @@ -1146,6 +1150,44 @@ server { } ``` +### 监控状态信息 + +通过 `nginx -V` 来查看是否有 `with-http_stub_status_module` 该模块。 + +> `nginx -V` 这里 `V` 是大写的,如果是小写的 `v` 即 `nginx -v`,则不会出现有哪些模块,只会出现 `nginx` 的版本 + +```nginx +location /nginx_status { + stub_status on; + access_log off; +} +``` + +通过 http://127.0.0.1/nginx_status 访问出现下面结果。 + +```bash +Active connections: 3 +server accepts handled requests + 7 7 5 +Reading: 0 Writing: 1 Waiting: 2 +``` + +1. 主动连接(第 1 行) + +当前与http建立的连接数,包括等待的客户端连接:3 + +2. 服务器接受处理的请求(第 2~3 行) + +接受的客户端连接总数目:7 +处理的客户端连接总数目:7 +客户端总的请求数目:5 + +3. 读取其它信(第 4 行) + +当前,nginx读请求连接 +当前,nginx写响应返回给客户端 +目前有多少空闲客户端请求连接 + ### 代理转发连接替换 ```nginx @@ -1155,7 +1197,7 @@ location ^~/api/upload { } ``` -### ssl配置 +### SSL 配置 超文本传输安全协议(缩写:HTTPS,英语:Hypertext Transfer Protocol Secure)是超文本传输协议和SSL/TLS的组合,用以提供加密通讯及对网络服务器身份的鉴定。HTTPS连接经常被用于万维网上的交易支付和企业信息系统中敏感信息的传输。HTTPS不应与在RFC 2660中定义的安全超文本传输协议(S-HTTP)相混。HTTPS 目前已经是所有注重隐私和安全的网站的首选,随着技术的不断发展,HTTPS 网站已不再是大型网站的专利,所有普通的个人站长和博客均可以自己动手搭建一个安全的加密的网站。 @@ -1397,6 +1439,53 @@ if ($rule_1 = "21"){ } ``` +### cockpit + +https://github.com/cockpit-project/cockpit + +```nginx +server{ +    listen 80; +    server_name cockpit.xxxxxxx.com; +    return 301 https://$server_name$request_uri; +} + +server { +    listen 443 ssl; +    server_name cockpit.xxxxxxx.com; + +    #ssl on; +    ssl_certificate /etc/nginx/cert/cockpit.xxxxxxx.com.pem; +    ssl_certificate_key /etc/nginx/cert/cockpit.xxxxxxx.com.key; + +    location / { +        root /; +        index index.html; +        proxy_redirect off; +        proxy_pass http://websocket; +        proxy_http_version 1.1; +        proxy_set_header Upgrade $http_upgrade; +        proxy_set_header Connection "upgrade"; +        proxy_set_header Host $http_host; +    } +} +``` + +这时输入域名,能看到登录页面,但登录后,显示不出内容,页面全白。这里要对 `cockpit.conf` 进行设置修改。 + +```bash +sudo vim /etc/cockpit/cockpit.conf +``` + +参照如下配置修改,注意域名替换为 `your_domain_host`: + +```toml +[WebService] +Origins = https://cockpit.xxxxxxx.com https://127.0.0.1:9090 +ProtocolHeader = X-Forwarded-Proto +AllowUnencrypted = true +``` + ## 错误问题 ```bash @@ -1422,6 +1511,10 @@ server { } ``` +## Nginx 模块 + +- [Nginx Office Hours](https://gitlab.com/rbdr/ngx_http_office_hours_filter_module) 一个 nginx 模块,允许您仅在办公时间内提供访问访问网站。 + ## 精品文章参考 - [负载均衡原理的解析](https://my.oschina.net/u/3341316/blog/877206) @@ -1434,3 +1527,17 @@ server { - [Nginx+Keepalived(双机热备)搭建高可用负载均衡环境(HA)](https://my.oschina.net/xshuai/blog/917097) - [Nginx 平滑升级](http://www.huxd.org/articles/2017/07/24/1500890692329.html) - [Nginx最新模块—ngx_http_mirror_module分析可以做版本发布前的预先验证,进行流量放大后的压测等等](https://mp.weixin.qq.com/s?__biz=MzIxNzg5ODE0OA==&mid=2247483708&idx=1&sn=90b0b1dccd9c337922a0588245277666&chksm=97f38cf7a08405e1928e0b46d923d630e529e7db8ac7ca2a91310a075986f8bcb2cee5b4953d#rd) + +## Contributors + +As always, thanks to our amazing contributors! + + + + + +Made with [action-contributors](https://github.com/jaywcjlove/github-action-contributors). + +## License + +Licensed under the MIT License. diff --git a/favicon.svg b/favicon.svg new file mode 100644 index 0000000..41e6312 --- /dev/null +++ b/favicon.svg @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/idoc.yml b/idoc.yml new file mode 100644 index 0000000..2e1df53 --- /dev/null +++ b/idoc.yml @@ -0,0 +1,25 @@ +site: "Nginx 入门学习笔记" +logo: "./favicon.svg" +favicon: "./favicon.svg" + +homepage: https://wangchujiang.com/nginx-tutorial/ + +menus: + 首页: index.html + 捐赠: + url: https://wangchujiang.com/#/sponsor + target: __blank + 应用: + url: https://wangchujiang.com/#/app + target: __blank + +editButton: + label: 在 GitHub 上编辑此页面 + url: https://github.com/jaywcjlove/nginx-tutorial/tree/master/ +footer: | + App • + Projects • + Sponsor • + More Apps

+ Released under the MIT License. Copyright © 2024 Kenny Wong
+ Generated by idoc v{{idocVersion}} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..2c759d5 --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "$schema": "https://json.schemastore.org/package.json", + "private": true, + "name": "nginx-tutorial", + "description": "Nginx 是一款面向性能设计的 HTTP 服务器,能反向代理 HTTP,HTTPS 和邮件相关(SMTP,POP3,IMAP)的协议链接。并且提供了负载均衡以及 HTTP 缓存。它的设计充分使用异步事件模型,削减上下文调度的开销,提高服务器并发能力。采用了模块化设计,提供了丰富模块的第三方模块。", + "homepage": "https://jaywcjlove.github.io/nginx-tutorial", + "version": "1.0.0", + "scripts": { + "start": "idoc --watch", + "build": "idoc" + }, + "repository": { + "type": "git", + "url": "https://github.com/jaywcjlove/nginx-tutorial" + }, + "keywords": [ + "nginx", + "tutorial", + "centos", + "Nginx 入门学习笔记" + ], + "dependencies": { + "idoc": "^1" + } +} \ No newline at end of file diff --git a/renovate.json b/renovate.json new file mode 100644 index 0000000..5fe6b17 --- /dev/null +++ b/renovate.json @@ -0,0 +1,11 @@ +{ + "extends": [ + "config:base" + ], + "packageRules": [ + { + "matchPackagePatterns": ["*"], + "rangeStrategy": "replace" + } + ] +}