nginx 配置踩坑實錄


http
{
include mime.types;
default_type application/octet-stream;
source_charset GB2312;
server_names_hash_bucket_size 256;
client_header_buffer_size 8k;#默認是4k
large_client_header_buffers 4 8k;

......

}

 

看到huoding.com上有比較好的帖子,於是理解並用自己的話來描述啦

加大client_header_buffer_size和large_client_header_buffers可以解決問題,但是為毛要兩個參數來控制呢?一個不久可以滿足要求了么?

client_header_buffer_size用來緩存請求頭,如果超過的話就會返回400錯誤了。但是如果絕大多數請求不會超過這個值的話,那么更大的請求頭可以受large_client_headers_buffers的影響。

還是貼nginx的原話吧

  • client_header_buffer_size: Sets buffer size for reading client request header. For most requests, a buffer of 1K bytes is enough. However, if a request includes long cookies, or comes from a WAP client, it may not fit into 1K. If a request line or a request header field does not fit into this buffer then larger buffers, configured by the large_client_header_buffers directive, are allocated.
  • large_client_header_buffers: Sets the maximum number and size of buffers used for reading large client request header. A request line cannot exceed the size of one buffer, or the 414 (Request-URI Too Large) error is returned to the client. A request header field cannot exceed the size of one buffer as well, or the 400 (Bad Request) error is returned to the client. Buffers are allocated only on demand. By default, the buffer size is equal to 8K bytes. If after the end of request processing a connection is transitioned into the keep-alive state, these buffers are released.

用兩個參數來控制緩存大小的原因是為了平衡內存資源和處理速度的矛盾。

 

查找原因后得知是nginx在轉發請求時會默認忽略帶有"_"下划線的頭信息

 

 

解決nginx轉發websocket報400錯誤

說明

由於個人服務器上面有多個項目,配置了二級域名,需要對二級域名進行轉發,在轉發工作這快采取了大名鼎鼎的nginx。在這之前所有的項目運行轉發都沒問題,然而今天在部署一個具有websocket通信的項目時,卻意外的報錯了,錯誤消息如下:

1failed: Error during WebSocket handshake: Unexpected response code: 400

。這個錯誤在本地測試環境以及訪問非nginx轉發都沒有問題,由此推斷出問題應該出現在nginx轉發這個環節。

於是,在google的幫助下,看到了socket.io 官方issues有關於這個問題的討論,鏈接:https://github.com/socketio/socket.io/issues/1942

解決方案

看了下討論區說的方案,問題出現在nginx的配置文件,需要修改nginx.conf文件。在linux終端中敲入vim /etc/nginx/nginx.conf,找到location這個位置,配置文件如下所示:

1server {
2        listen       80;
3        server_name  school.godotdotdot.com;
4        charset utf-8;
5
6        location / {
7            proxy_pass http://127.0.0.1:3000;
8            proxy_set_header Host $host;
9            proxy_http_version 1.1;
10            proxy_set_header Upgrade $http_upgrade;
11            proxy_set_header Connection "upgrade";
12            proxy_set_header X-Real-IP $remote_addr;
13            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
14            proxy_connect_timeout 60;
15            proxy_read_timeout 600;
16            proxy_send_timeout 600;
17        }
18
19        error_page   500 502 503 504  /50x.html;
20        location = /50x.html {
21            root   html;
22        }
23
24    }

其中最重要的是下面這三行

1proxy_http_version 1.1;
2proxy_set_header Upgrade $http_upgrade;
3proxy_set_header Connection "upgrade";

其中第一行是告訴nginx使用HTTP/1.1通信協議,這是websoket必須要使用的協議。

第二行和第三行告訴nginx,當它想要使用WebSocket時,響應http升級請求。

 

nginx反向代理Authorization請求頭問題解決

在前后端分離的開發中使用nginx做反向代理去請求服務器地址時,發現自定義的header請求頭Authorization信息丟失了,在網上找了很多資料都不是我所需要的,現解決后記錄如下:

修改nginx的nginx.conf配置文件,添加如下紅框所示內容。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM