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:4563http://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


assassin_cike
1.3k 声望74 粉丝

生活不是得过且过