1
0

运维:Nginx 常用配置

This commit is contained in:
周中平 2022-08-08 16:03:10 +08:00
parent a0729bf524
commit 8b1dc94436
No known key found for this signature in database
GPG Key ID: B1DF9DD42D8E00DC
3 changed files with 140 additions and 2 deletions

View File

@ -6,7 +6,7 @@ data: 2022年8月5日
熟练使用命令行是一种常常被忽视,或被认为难以掌握的技能,但实际上,它会提高你作为工程师的灵活性以及生产力。本文是一份我在 Linux 上工作时,发现的一些命令行使用技巧的摘要。有些技巧非常基础,而另一些则相当复杂,甚至晦涩难懂。这篇文章并不长,但当你能够熟练掌握这里列出的所有技巧时,你就学会了很多关于命令行的东西了。
这篇文章是[许多作者和译者](AUTHORS.md)共同的成果。这里的部分内容[首次](http://www.quora.com/What-are-some-lesser-known-but-useful-Unix-commands)[出现](http://www.quora.com/What-are-the-most-useful-Swiss-army-knife-one-liners-on-Unix)于 [Quora](http://www.quora.com/What-are-some-time-saving-tips-that-every-Linux-user-should-know),但已经迁移到了 Github并由众多高手做出了许多改进。如果你在本文中发现了错误或者存在可以改善的地方[**贡献你的一份力量**](/CONTRIBUTING.md)
这篇文章是许多作者和译者共同的成果。这里的部分内容[首次](http://www.quora.com/What-are-some-lesser-known-but-useful-Unix-commands)[出现](http://www.quora.com/What-are-the-most-useful-Swiss-army-knife-one-liners-on-Unix)于 [Quora](http://www.quora.com/What-are-some-time-saving-tips-that-every-Linux-user-should-know),但已经迁移到了 Github并由众多高手做出了许多改进。如果你在本文中发现了错误或者存在可以改善的地方请贡献你的一份力量。
## 前言

View File

@ -297,7 +297,7 @@ const config = {
theme: lightCodeTheme,
darkTheme: darkCodeTheme,
defaultLanguage: "markdown",
additionalLanguages: ["java", "git"],
additionalLanguages: ["java", "git","nginx"],
},
}),
};

View File

@ -0,0 +1,138 @@
---
id: Nginx 常用配置
title: Nginx 常用配置
data: 2022年08月08日
---
在线配置网站:<https://nginxconfig.io/>
## 侦听端口
```nginx
server {
# Standard HTTP Protocol
listen 80;
# Standard HTTPS Protocol
listen 443 ssl;
# For http2
listen 443 ssl http2;
# Listen on 80 using IPv6
listen [::]:80;
# Listen only on using IPv6
listen [::]:80 ipv6only=on;
}
```
## 访问日志
```nginx
server {
# Relative or full path to log file
access_log /path/to/file.log;
# Turn 'on' or 'off'
access_log on;
}
```
## 域名
```nginx
server {
# Listen to yourdomain.com
server_name yourdomain.com;
# Listen to multiple domains server_name yourdomain.com www.yourdomain.com;
# Listen to all domains
server_name *.yourdomain.com;
# Listen to all top-level domains
server_name yourdomain.*;
# Listen to unspecified Hostnames (Listens to IP address itself)
server_name "";
}
```
## 静态资产
```nginx
server {
listen 80;
server_name yourdomain.com;
location / {
root /path/to/website;
}
}
```
## 重定向
```nginx
server {
listen 80;
server_name www.yourdomain.com;
return 301 http://yourdomain.com$request_uri;
}
server {
listen 80;
server_name www.yourdomain.com;
location /redirect-url {
return 301 http://otherdomain.com;
}
}
```
## 反向代理
```nginx
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://0.0.0.0:3000;
# where 0.0.0.0:3000 is your application server (Ex: node.js) bound on 0.0.0.0 listening on port 3000
}
}
```
## 负载均衡
```nginx
upstream node_js {
server 0.0.0.0:3000;
server 0.0.0.0:4000;
server 123.131.121.122;
}
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://node_js;
}
}
```
## SSL 协议
```nginx
server {
listen 443 ssl;
server_name yourdomain.com;
ssl on;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/privatekey.pem;
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /path/to/fullchain.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_timeout 1h;
ssl_session_cache shared:SSL:50m;
add_header Strict-Transport-Security max-age=15768000;
}
# Permanent Redirect for HTTP to HTTPS
server
{
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}
```