博客 / 詳情

返回

nginx 隨記

1. 控制枱報錯:SyntaxError: Unexpected token '<'

使用 Pause on caught exceptions 斷點不到源碼 即使我開了source map,因為項目使用了負載均衡後出現的問題,單nginx部署是沒問題的,那麼問題應該出在負載均衡上,按照nginx + 前後端分離 負載均衡配置了ip_hash後,可以正常的訪問了。項目分別拉取到不同的服務器進行構建,雖然代碼一致但是構建出來的文件的hash值不同,所以才回報錯。

2. nginx默認上傳文件大小限制為1M

nginx-1.26.2
在我用nginx做前端接口代理的時候發現上傳的文件稍微大點就報錯,原來nginx默認對上傳文件的大小限制為1M,怎樣取消這個限制呢?

        location ~ ^/(v1|api) {
            proxy_pass http://127.0.0.1:9380;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size 0; // 取消限制
        }

3. nginx 同一個域名下配置不同的路徑訪問不同的項目

兩個項目都是用的umijs作為腳手架

geek 項目的umirc

import { defineConfig } from "umi";

export default defineConfig({
  base: "/geek/", // 關鍵配置
  publicPath: "./",
  routes: [
    { path: "/", component: "index" },
    { path: "/docs", component: "docs" },
  ],
  npmClient: "pnpm",
  proxy: {
    "/upload": {
      target: "http://127.0.0.1:7488/",
      changeOrigin: true,
      ws: true,
      logger: console,
      // pathRewrite: { '^/v1': '/v1' },
    },
  },
});

我在本地做了如下配置

    server {
        listen 4563;
        server_name _;

        gzip on;
        gzip_min_length 1k;
        gzip_comp_level 9;
        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        gzip_vary on;
        gzip_disable "MSIE [1-6]\.";


        location /geek {
            alias D:/work/upload-then-show-markdown/dist/;
            index index.html;
            try_files $uri $uri/ /index.html;
        }

        location / {
            root D:/work/my-aaa/web/dist;
            index index.html;
            try_files $uri $uri/ /index.html;
        }


        # Cache-Control: max-age~@~AExpires
        location ~ ^/static/(css|js|media)/ {
            expires 10y;
            access_log off;
        }
    }

分別通過http://localhost:4563,http://localhost:4563/geek/ 訪問兩個項目都沒問題,但是在服務器上不行,配置如下


upstream geek_web_https {
    ip_hash;
    server 127.0.0.3:4545 max_fails=6 fail_timeout=1;
    server 127.0.0.4:4545 max_fails=6 fail_timeout=1;
    server 127.0.0.5:4545 max_fails=6 fail_timeout=1;
    server 127.0.0.6:4545 max_fails=6 fail_timeout=1;
    keepalive 100;
}

server {
    listen 443 ssl;
    server_name _;
    ssl_certificate  /root/geek.https.cer;
    ssl_certificate_key   /root/geek.https.key;


    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 9;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";

    location /geek {
        alias /aaa/dist/;
        index index.html;
        try_files $uri $uri/ /index.html;
    }

    location / {
      proxy_pass https://geek_web_https;
      include proxy.conf;
      index index.html;
      try_files $uri $uri/ /index.html;
    }


    # Cache-Control: max-age~@~AExpires
    location ~ ^/static/(css|js|media)/ {
        expires 10y;
        access_log off;
    }
    location ~ .*\.(?:htm|html)$ {
        add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
    }

}

跟我本地的最大區別是服務器做了負載均衡,參考umi 非根路徑部署 將 location /geek 改為 ^~ /geek ,可以成功的訪問到另一個項目了。起效的原因應該是 ^~ /geek 優先級高於 /

location 路徑配置優先級

nginx 官方location優先級解釋
參考:
🚀終於理解了Nginx配置中location規則的優先級問題
一份簡單夠用的 Nginx Location 配置講解
Nginx location priority

4. err_incomplete_chunked_encoding 200 (ok)

使用fetch sse 獲取數據比較多然後就報錯了,err_incomplete_chunked_encoding 200 (ok)
解決辦法:

        location ~ ^/(v1|api) {
            proxy_buffer_size 1024k; #設置代理服務器(nginx)保存用户頭信息的緩衝區大小
            proxy_buffers 16 1024k; #proxy_buffers緩衝區,網頁平均在32k以下的設置
            proxy_busy_buffers_size 2048k; #高負荷下緩衝大小(proxy_buffers*2)
            proxy_temp_file_write_size 2048k;#設定緩存文件夾大小,大於這個值,將從upstream服務器傳
        }
user avatar kasong 頭像 hachimei 頭像
2 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.