NGINX 重定向返回 414 请求 URI 太大

NGINX 重定向返回 414 请求 URI 太大

我不是 NGINX 专家,我正在尝试将整个网站从一个域重定向到另一个域。

为了在本地测试解决方案,我使用了两个不同的 URL:

  • 虚假旧域名 -> 127.0.0.3
  • 虚假新域名 -> 127.0.0.1

但是当我使用下面的代码时:

server {
    listen 80;
    listen [::]:80;
    server_name 127.0.0.3;
    return 301 $scheme://127.0.0.1$request_uri;
}

我看到这个错误:

414 请求 URI 太大

在此处输入图片描述

我该如何解决它?

NGINX 位于 Docker 容器内,管理两种不同的服务,如下所示。

server {
    listen 80;
    listen [::]:80;
    #server_name  domain.me www.domain.me;
    server_name  127.0.0.1; # FOR STAGING ONLY

    error_log  /var/log/nginx/error.log warn;
    access_log /var/log/nginx/access.log combined;

    location / {
        proxy_pass http://website:8301; # The port must be the same of PROJECT_PORT
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        # max uploadable file size
        client_max_body_size 4G;
    }

    location /static/ {
        # static folder on the server
        alias /app/web/static-folder/;
    }

    location /media/ {
        # media folder on the server
        alias /app/web/media-folder/;
    }
}

server {
    #server_name  geoserver.domain.me www.geoserver.domain.me;
    server_name 127.0.0.2; # FOR STAGING ONLY

    listen 80;
    listen [::]:80;

    server_tokens off;

    root /usr/share/nginx/html;
    index index.html;

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types
        text/plain
        text/css
        application/json
        application/javascript
        application/x-javascript
        text/xml application/xm
        application/xml+rss
        text/javascript;

    underscores_in_headers on;
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_buffering on;
    proxy_buffer_size 1k;
    proxy_buffers 24 4k;
    proxy_busy_buffers_size 8k;
    proxy_max_temp_file_size 2048m;
    proxy_temp_file_write_size 32k;

    location / {
            deny all;
            return 301 /geoserver;
    }

    location /geoserver {
        proxy_pass         http://geoserver:8080/geoserver;
    }
}

编辑:我报告以下完整的会议:

server {
    listen 80;
    listen [::]:80;
    #server_name  domain.me www.domain.me;
    server_name  127.0.0.3; # FOR STAGING ONLY
    return 301 $scheme://127.0.0.1$request_uri;

    error_log  /var/log/nginx/error.log error;
    access_log /var/log/nginx/access.log combined;

    location / {
        proxy_pass http://website:8301; # The port must be the same of PROJECT_PORT
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
        # max uploadable file size
        client_max_body_size 4G;
    }

    location /static/ {
        # static folder on the server
        alias /app/web/static-folder/;
    }

    location /media/ {
        # media folder on the server
        alias /app/web/media-folder/;
    }
}

server {
    #server_name  geoserver.domain.me www.geoserver.domain.me;
    server_name 127.0.0.2; # FOR STAGING ONLY

    listen 80;
    listen [::]:80;

    server_tokens off;

    root /usr/share/nginx/html;
    index index.html;

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types
        text/plain
        text/css
        application/json
        application/javascript
        application/x-javascript
        text/xml application/xm
        application/xml+rss
        text/javascript;

    underscores_in_headers on;
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_buffering on;
    proxy_buffer_size 1k;
    proxy_buffers 24 4k;
    proxy_busy_buffers_size 8k;
    proxy_max_temp_file_size 2048m;
    proxy_temp_file_write_size 32k;

    location / {
            deny all;
            return 301 /geoserver;
    }

    location /geoserver {
        proxy_pass         http://geoserver:8080/geoserver;
    }
}

答案1

查看浏览器中的 URL。

您正在重定向//domain。您想重定向至https://domain/

当您的浏览器连接到 时127.0.0.3,它会重定向到/127.0.0.1。它应该重定向到https://127.0.0.1

在这种情况下,使用浏览器调试工具或 curl 来检查实际的响应与预期响应相比。

相关内容