Nginx 配置清单(一篇够用)

x33g5p2x  于2021-11-03 转载在 其他  
字(2.2k)|赞(0)|评价(0)|浏览(541)

Nginx 是一个高性能的 HTTP 和反向代理 web 服务器,同时也提供了 IMAP/POP3/SMTP 服务,其因丰富的功能集、稳定性、示例配置文件和低系统资源的消耗受到了开发者的欢迎。

本文,我们总结了一些常用的 Nginx 配置代码,希望对大家有所帮助。

侦听端口

  1. server {  
  2. # Standard HTTP Protocol  
  3. listen 80;  
  4. # Standard HTTPS Protocol  
  5. listen 443 ssl;  
  6. # For http2  
  7. listen 443 ssl http2;  
  8. # Listen on 80 using IPv6  
  9. listen [::]:80;  
  10. # Listen only on using IPv6  
  11. listen [::]:80 ipv6only=on;  
  12. }

访问日志

  1. server {  
  2. # Relative or full path to log file  
  3. access_log /path/to/file.log;  
  4. # Turn 'on' or 'off'    
  5. access_log on;  
  6. }

域名

  1. server {  
  2. # Listen to yourdomain.com  
  3. server_name yourdomain.com;  
  4. # Listen to multiple domains server_name yourdomain.com www.yourdomain.com;  
  5. # Listen to all domains  
  6. server_name *.yourdomain.com;  
  7. # Listen to all top-level domains  
  8. server_name yourdomain.*;  
  9. # Listen to unspecified Hostnames (Listens to IP address itself)  
  10. server_name "";  
  11. }

静态资产

  1. server {  
  2. listen 80;  
  3. server_name yourdomain.com;  
  4. location / {  
  5. root /path/to/website;  
  6. }  
  7. }

重定向

  1. server {  
  2. listen 80;  
  3. server_name www.yourdomain.com;  
  4. return 301 http://yourdomain.com$request_uri;  
  5. }  
  6. server {  
  7. listen 80;  
  8. server_name www.yourdomain.com;  
  9. location /redirect-url {  
  10. return 301 http://otherdomain.com;  
  11. }  
  12. }

反向代理

  1. server {  
  2. listen 80;  
  3. server_name yourdomain.com;  
  4. location / {  
  5. proxy_pass http://0.0.0.0:3000;  
  6. # where 0.0.0.0:3000 is your application server (Ex: node.js) bound on 0.0.0.0 listening on port 3000  
  7. }  
  8. }

负载均衡

  1. upstream node_js {  
  2. server 0.0.0.0:3000;  
  3. server 0.0.0.0:4000;  
  4. server 123.131.121.122;  
  5. }  
  6. server {  
  7. listen 80;  
  8. server_name yourdomain.com;  
  9. location / {  
  10. proxy_pass http://node_js;  
  11. }  
  12. }

SSL 协议

  1. server {  
  2. listen 443 ssl;  
  3. server_name yourdomain.com;  
  4. ssl on;  
  5. ssl_certificate /path/to/cert.pem;  
  6. ssl_certificate_key /path/to/privatekey.pem;  
  7. ssl_stapling on;  
  8. ssl_stapling_verify on;  
  9. ssl_trusted_certificate /path/to/fullchain.pem;  
  10. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  
  11. ssl_session_timeout 1h;  
  12. ssl_session_cache shared:SSL:50m;  
  13. add_header Strict-Transport-Security max-age=15768000;  
  14. }  
  15. # Permanent Redirect for HTTP to HTTPS  
  16. server   
  17. {  
  18. listen 80;  
  19. server_name yourdomain.com;  
  20. return 301 https://$host$request_uri;  
  21. }

其实可以采用可视化的方式对 Nginx 进行配置,我在 GitHub 上发现了一款可以一键生成 Nginx 配置的神器,相当给力。

先来看看它都支持什么功能的配置:反向代理、HTTPS、HTTP/2、IPv6, 缓存、WordPress、CDN、Node.js 支持、 Python (Django) 服务器等等。

如果你想在线进行配置,只需要打开网站:https://nginxconfig.io/,按照自己的需求进行操作就行了。


图片

选择你的场景,填写好参数,系统就会自动生成配置文件。

开源地址:

github.com/digitalocean/nginxconfig.io

网站:

digitalocean.com/community/tools/nginx
*感谢阅读,希望对你有所帮助 :) *

来源:vishnu.hashnode.dev/nginx-cheatsheet

相关文章