Nginx 常用配置清单

小灰灰 2022-10-21 12:53 281阅读 0赞

bea82f4e5bb1d154f05231930651ab1f.png

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

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/,按照自己的需求进行操作就行了。

4aa27cf07aeaf2cdbef16f4d7c3a81fc.png 图片

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

开源地址:github.com/digitalocean/nginxconfig.io

网站:digitalocean.com/community/tools/nginx

推荐好文

强大,10k+点赞的 SpringBoot 后台管理系统竟然出了详细教程!

分享一套基于SpringBoot和Vue的企业级中后台开源项目,代码很规范!

能挣钱的,开源 SpringBoot 商城系统,功能超全,超漂亮1e96b367058c6f21d9f786fffd6bfdc6.png

发表评论

表情:
评论列表 (有 0 条评论,281人围观)

还没有评论,来说两句吧...

相关阅读

    相关 命令清单

    仅供个人记录,没有参考价值,可直接忽略 已损坏,无法打开,您应该将它移到废纸篓 macOS Catalina下,即便已通过在终端中输入命令 sudo spctl --m

    相关 Nginx 配置

    Nginx是web服务、反向代理、缓存、负载平衡、媒体流等方面的开源软件,同时也提供了 IMAP/POP3/SMTP 服务,其因丰富的功能集、稳定性、示例配置文件和低系统资源的