Nginx configures https certificate, port 80 automatically jumps to 443, www automatically jumps, reverse proxy configuration

Certificate application:

Use the free certificate application website https://freessl.cn/ . For specific installation tutorials, please refer to: https://blog.freessl.cn/acme-quick-start/ . After installation, a certificate will be generated under a certain path. You only need to configure the corresponding certificate private key and certificate chain to nginx.

Configuration example:

server {
    
    
  # 1.定义端口,这里80443端口放在一起,方便统一处理跳转
  listen 80;
  listen [::]:80;
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  
  # 2.定义域名
  server_name mcos.top www.bbs.com;
  access_log /data/wwwlogs/bbs.com_nginx.log combined;
  error_log /data/wwwlogs/bbs.com_error_nginx.log error;
  index index.html index.htm index.php;
  root /data/wwwroot/bbs.com;

  # 3. 带www等子域名前缀的统一跳转为无前缀的 bbs.com
  if ($host != bbs.com) {
    
      return 301 $scheme://bbs.com$request_uri;  }
  # 4.将http跳转为https。注意34步骤顺序不要搞混,先处理www,再处理ssl
  if ($ssl_protocol = "") {
    
     return 301 https://$host$request_uri; }

  # 5.第四处理完后再添加SSL配置
  ssl_certificate /root/.acme.sh/bbs.com_ecc/bbs.com.cer;
  ssl_certificate_key /root/.acme.sh/bbs.com_ecc/bbs.com.key;
  ssl_protocols TLSv1.2 TLSv1.3;
  ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1;
  ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256;
  ssl_conf_command Ciphersuites TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256;
  ssl_conf_command Options PrioritizeChaCha;
  ssl_prefer_server_ciphers on;
  ssl_session_timeout 10m;
  ssl_session_cache shared:SSL:10m;
  ssl_buffer_size 2k;
  add_header Strict-Transport-Security max-age=15768000;
  ssl_stapling on;
  ssl_stapling_verify on;

  # 其它配置
  include /usr/local/nginx/conf/rewrite/other.conf;
  #error_page 404 /404.html;
  #error_page 502 /502.html;
  
 ########主业务配置,示例:###########
 
   #对 "/" 启用反向代理
  location / {
    
    
    proxy_pass http://127.0.0.1:8081;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    
    #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
    #以下是一些反向代理的配置,可选。
    proxy_set_header Host $host;
    #允许客户端请求的最大单文件字节数
    client_max_body_size 50m;
    #缓冲区代理缓冲用户端请求的最大字节数, #如果把它设置为比较大的数值,例如256k,那么,无论使用firefox还是IE浏览器,来提交任意小于256k的图片,都很正常。如果注释该指令,使用默认的client_body_buffer_size设置,也就是操作系统页面大小的两倍,8k或者16k,问题就出现了。
    #无论使用firefox4.0还是IE8.0,提交一个比较大,200k左右的图片,都返回500 Internal Server Error错误
    client_body_buffer_size 128k;
    #表示使nginx阻止HTTP应答代码为400或者更高的应答。
    proxy_intercept_errors on;
    #后端服务器连接的超时时间_发起握手等候响应超时时间
    #nginx跟后端服务器连接超时时间(代理连接超时)
    proxy_connect_timeout 90;
    #后端服务器数据回传时间(代理发送超时)
    #后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
    proxy_send_timeout 90;
    #连接成功后,后端服务器响应时间(代理接收超时)
    #连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)
    proxy_read_timeout 90;
    #设置代理服务器(nginx)保存用户头信息的缓冲区大小
    #设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头,默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小,不过可以将其设置为更小
    proxy_buffer_size 4k;
    #proxy_buffers缓冲区,网页平均在32k以下的设置
    #设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认情况也为分页大小,根据操作系统的不同可能是4k或者8k
    proxy_buffers 4 32k;
    #高负荷下缓冲大小(proxy_buffers*2
    proxy_busy_buffers_size 64k;
    #设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长
    #设定缓存文件夹大小,大于这个值,将从upstream服务器传
    proxy_temp_file_write_size 64k;
  }

  #location ~ [^/]\.php(/|$) {
    
    
  #  #fastcgi_pass remote_php_ip:9000;
  #  fastcgi_pass unix:/dev/shm/php-cgi.sock;
  #  fastcgi_index index.php;
  #  include fastcgi.conf;
  #}

  #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    
    
  #  expires 30d;
  #  access_log off;
  #}
  #location ~ .*\.(js|css)?$ {
    
    
  #  expires 7d;
  #  access_log off;
  #}
  location ~ /(\.user\.ini|\.ht|\.git|\.svn|\.project|LICENSE|README\.md) {
    
    
    deny all;
  }
  location /.well-known {
    
    
    allow all;
  }
}

Guess you like

Origin blog.csdn.net/u014438244/article/details/130930441