0%

Nginx常见问题

本文记录了使用 Nginx 遇到的一些问题

强制Http跳转到Https访问

分为两种情况:

  1. http://jaceding.github.com/跳转https://jaceding.github.com/
  2. http://jaceding.github.com:18182/跳转https://jaceding.github.com:18182/

第一种情况

通过监听80端口,然后重定向到Https(默认443端口)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
listen 80;
server_name jaceding.github.com;
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl http2;
server_name jaceding.github.com;
ssl_protocols TLSv1.3 TLSv1.2;
ssl_ciphers EECDH:+CHACHA20:+aRSA:+AES256:!NULL:!ARIA:!CAMELLIA:!SHA:!MD5;
server_tokens off;

access_log /var/log/nginx/page_access main;
error_log /var/log/nginx/page_error;
ssl_certificate /usr/cert/wanxin/wanxinwangluo.com.cer;
ssl_certificate_key /usr/cert/wanxin/wanxinwangluo.com.key;

location / {
root html;
index index.html index.htm;
}
}

或者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
listen 80;
server_name jaceding.github.com;
rewrite ^(.*)$ https://${server_name}$1 permanent;
}

server {
listen 443 ssl http2;
server_name jaceding.github.com;
ssl_protocols TLSv1.3 TLSv1.2;
ssl_ciphers EECDH:+CHACHA20:+aRSA:+AES256:!NULL:!ARIA:!CAMELLIA:!SHA:!MD5;
server_tokens off;

access_log /var/log/nginx/page_access main;
error_log /var/log/nginx/page_error;
ssl_certificate /usr/cert/wanxin/wanxinwangluo.com.cer;
ssl_certificate_key /usr/cert/wanxin/wanxinwangluo.com.key;

location / {
root html;
index index.html index.htm;
}
}

第二种情况

当网站只允许https访问时,用http访问时nginx会报出497错误码。

通过 error_page 命令将497状态码的链接重定向到https://jaceding.github.com:18182/这个域名上

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
39
40
upstream libreSpeed {
server 10.86.52.77:80;
server 10.86.122.208:80;
keepalive 64;
}

server {
listen 18182 ssl http2;
server_name jaceding.github.com;
ssl_protocols TLSv1.3 TLSv1.2;
ssl_ciphers EECDH:+CHACHA20:+aRSA:+AES256:!NULL:!ARIA:!CAMELLIA:!SHA:!MD5;
server_tokens off;

access_log /var/log/nginx/libreSpeed_access main;
error_log /var/log/nginx/libreSpeed_error;
ssl_certificate /usr/cert/wanxin/wanxinwangluo.com.cer;
ssl_certificate_key /usr/cert/wanxin/wanxinwangluo.com.key;

error_page 497 https://$host:$server_port$uri$is_args$args;

location / {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header Cache-Control no-cache;
add_header X-XSS-Protection "1;mode=block";
add_header x-frame-options SAMEORIGIN;
proxy_hide_header X-Powered-By;
proxy_hide_header "cache-control";
proxy_hide_header Expires;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Nginx-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_pass http://libreSpeed;
}
}

两次post请求过快出现499

HTTP 状态码 499 的意思是:客户端发起请求后,一段时间内没有收到代理服务器的应答,导致连接失败

有两种可能:

  1. 代理服务器认为客户端发起的请求过于危险,所以主动给断了。(例如两次Post请求过快)
  2. 代理服务器实在么得办法连接到其他服务,导致timeout。

针对第一种情况,可以在 Nginx 中添加以下配置:

1
proxy_ignore_client_abort on;   # 代理服务器不主动关闭连接

header 参数丢失

Nginx 做反向代理时,请求头中的 key 如果包含下划线 “_” 时,会自动忽略掉。

可以在配置文件中 http 部分添加以下配置:

1
underscores_in_headers on;
坚持原创技术分享,您的支持将鼓励我继续创作!