PeaceSheep's blog PeaceSheep's blog
首页
  • 分类
  • 标签
  • 归档
相关链接
提建议&咨询&赞赏
GitHub (opens new window)

PeaceSheep

以最简洁、易懂的话解决问题
首页
  • 分类
  • 标签
  • 归档
相关链接
提建议&咨询&赞赏
GitHub (opens new window)
  • 01 说明
  • 环境安装与配置

  • 常用命令与配置文件

    • Nginx多种配置样例,HTTPS配置与HTTP自动重定向
      • HTTPS,HTTP自动重定向
      • 静态文件
      • 转发请求
      • 文件下载
      • 大小相关
        • 设置最大请求体
      • 超时设置
        • clientheadertimeout
        • clientbodytimeout
        • keepalive_timeout
        • lingering_timeout
        • resolver_timeout
        • proxyconnecttimeout
        • proxyreadtimeout
        • proxysendtimeout
        • proxyupstreamfailtimeout(failtimeout)
    • tmux配置文件,解决鼠标不能用等问题
    • ubuntu常用命令
    • Nginx允许特定域名跨域(解决明明200但是仍然CORS错误)
    • apt-get使用代理
  • 常用代码

  • 常用操作
  • 常用命令与配置文件
PeaceSheep
2022-04-02
目录

Nginx多种配置样例,HTTPS配置与HTTP自动重定向

这是一份NGINX的样例配置。可以直接参照这些样例进行配置。

# HTTPS,HTTP自动重定向

    server { # 该server表示80端口自动重定向
        listen 80;
        server_name _; # 根据需要替换你的server_name
        rewrite ^(.*)$ https://$host$1 permanent;
    }
    server {
        listen       443 default_server;  #监听443端口
        listen       [::]:443 default_server;
        server_name  _;
        ssl on;
        ssl_certificate  /root/card/XXX.pem;   #ssl证书的pem文件路径
        ssl_certificate_key /root/card/XXX.key;  #ssl证书的key文件路径
        include /etc/nginx/default.d/*.conf;
        location / { # 静态文件
            root /root/dist;
            index index.html;
            try_files $uri $uri/ /index.html; #解决VUE刷新404问题
        }
        location /socket{  # websocket
            proxy_pass http://localhost:8000/socket;
            proxy_connect_timeout 60s;
            proxy_read_timeout 300s;
            proxy_send_timeout 60s;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Host $host;
        }
       
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

# 静态文件

location / { # 静态文件
    root /root/dist;
    index index.html;
    try_files $uri $uri/ /index.html; #解决VUE刷新404问题
}
1
2
3
4
5

# 转发请求

location /api{
    proxy_pass http://localhost:8001/api; # 表示后端重定向端口
}
1
2
3

# 文件下载

 location / {
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on; 
    charset utf-8; # 修复中文文件乱码问题
}
1
2
3
4
5
6

使用这种方式会自动生成如下页面:

# 大小相关

# 设置最大请求体

如果您的服务器有上传文件功能,且出现了413错误,有可能是请求体超过大小限制导致的,需要添加如下配置:

client_max_body_size 8m;
1

# 超时设置

# client_header_timeout

语法 client_header_timeout time 默认值 60s 上下文 http server 说明 指定等待client发送一个请求头的超时时间(例如:GET / HTTP/1.1).仅当在一次read中,没有收到请求头,才会算成超时。如果在超时时间内,client没发送任何东西,nginx返回HTTP状态码408(“Request timed out”)

# client_body_timeout

语法 client_body_timeout time 默认值 60s 上下文 http server location 说明 该指令设置请求体(request body)的读超时时间。仅当在一次readstep中,没有得到请求体,就会设为超时。超时后,nginx返回HTTP状态码408(“Request timed out”)

# keepalive_timeout

语法 keepalive_timeout timeout [ header_timeout ] 默认值 75s 上下文 http server location 说明 第一个参数指定了与client的keep-alive连接超时时间。服务器将会在这个时间后关闭连接。可选的第二个参数指定了在响应头Keep-Alive: timeout=time中的time值。这个头能够让一些浏览器主动关闭连接,这样服务器就不必要去关闭连接了。没有这个参数,nginx不会发送Keep-Alive响应头(尽管并不是由这个头来决定连接是否“keep-alive”) 两个参数的值可并不相同

注意不同浏览器怎么处理“keep-alive”头:

  • MSIE和Opera忽略掉"Keep-Alive: timeout=< N >" header.
  • MSIE保持连接大约60-65秒,然后发送TCP RST
  • Opera永久保持长连接
  • Mozilla保持N+x(x为大概0-10的随机数)
  • Konqueror保持长连接N秒

# lingering_timeout

语法 lingering_timeout time 默认值 5s 上下文 http server location 说明 lingering_close生效后,在关闭连接前,会检测是否有用户发送的数据到达服务器,如果超过lingering_timeout时间后还没有数据可读,就直接关闭连接;否则,必须在读取完连接缓冲区上的数据并丢弃掉后才会关闭连接。

# resolver_timeout

语法 resolver_timeout time 默认值 30s 上下文 http server location 说明 该指令设置DNS解析超时时间

# proxy_connect_timeout

语法 proxy_connect_timeout time 默认值 60s 上下文 http server location 说明 该指令设置与upstream server的连接超时时间,有必要记住,这个超时不能超过75秒。 这个不是等待后端返回页面的时间,那是由proxy_read_timeout声明的。如果你的upstream服务器起来了,但是hanging住了(例如,没有足够的线程处理请求,所以把你的请求放到请求池里稍后处理),那么这个声明是没有用的,由于与upstream服务器的连接已经建立了。

# proxy_read_timeout

语法 proxy_read_timeout time 默认值 60s 上下文 http server location 说明 该指令设置与代理服务器的读超时时间。它决定了nginx会等待多长时间来获得请求的响应。这个时间不是获得整个response的时间,而是两次reading操作的时间。

# proxy_send_timeout

语法 proxy_send_timeout time 默认值 60s 上下文 http server location 说明 这个指定设置了发送请求给upstream服务器的超时时间。超时设置不是为了整个发送期间,而是在两次write操作期间。如果超时后,upstream没有收到新的数据,nginx会关闭连接

# proxy_upstream_fail_timeout(fail_timeout)

语法 server address [fail_timeout=30s] 默认值 10s 上下文 upstream 说明 Upstream模块下 server指令的参数,设置了某一个upstream后端失败了指定次数(max_fails)后,该后端不可操作的时间,默认为10秒

参考资料

https://www.cnblogs.com/lemon-flm/p/8352194.html (opens new window)

编辑 (opens new window)
#nginx
上次更新: 2025/05/28, 16:40:57
nodejs、yarn安装
tmux配置文件,解决鼠标不能用等问题

← nodejs、yarn安装 tmux配置文件,解决鼠标不能用等问题→

最近更新
01
愚蠢错误收集
05-29
02
ubuntu安装g++显示已有但是输入g++又找不到命令
04-15
03
使用cloudflare-r2搭建webdav
04-08
更多文章>
Theme by Vdoing | Copyright © 2022-2025 PeaceSheep
冀ICP备2022004632号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式