ikun派cv工程师 2024-06-02 22:35 采纳率: 0%
浏览 7

关于#前端#的问题:'"$http_user_agent" "$http_x_forwarded_for"'(相关搜索:跨域问题)


user root;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/json;
     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;



    keepalive_timeout  65;

    server {
        listen       80 ;
        server_name  localhost;
        # 指定前端项目所在的位置
        location / {
            root   /etc/nginx/monitor;
            index  index.html index.htm;
          #  try_files $uri $uri/ /index.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /etc/nginx;
        }

    location ~ /\. {
        deny all;
    }

        # 反向代理到后端
        location /api {
            default_type  application/json;
            #internal;  
            keepalive_timeout   30s;
            keepalive_requests  1000;
            #支持keep-alive  
            proxy_http_version 1.1;
            rewrite /api(/.*) $1 break;
            proxy_pass_request_headers on;
            #more_clear_input_headers Accept-Encoding;  
            proxy_next_upstream error timeout;
            proxy_pass http://jdkhouduan:8888;
            #proxy_pass http://backend;
            # 添加跨域头部
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;

        # 针对 OPTIONS 请求返回 204 No Content,以支持预检请求(preflight)
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain; charset=utf-8';
            add_header 'Content-Length' 0;
            return 204;
        }
        }
    }

#     upstream backend {
#         server 127.0.0.1:8888 max_fails=5 fail_timeout=10s weight=1;
#         #server 127.0.0.1:8082 max_fails=5 fail_timeout=10s weight=1;
#     }
}

为什么配置了跨域请求头,还是解决不了跨域问题

  • 写回答

3条回答 默认 最新

  • 檀越@新空间 2024-06-02 22:35
    关注

    晚上好🌙🌙🌙
    本答案参考ChatGPT-3.5

    在您的Nginx配置中,您已经添加了必要的跨域请求头来处理CORS(跨源资源共享)问题。但是,如果仍然遇到跨域问题,可能是以下几个原因导致的:

    1. 服务器端响应

      • 确保后端API(如http://jdkhouduan:8888)在响应时也允许来自特定源的跨域请求。后端可能需要设置相应的响应头,如Access-Control-Allow-Origin
    2. 浏览器缓存

      • 浏览器可能会缓存跨域错误,尝试清除浏览器缓存或者使用开发者工具的网络面板检查请求是否成功。
    3. 代理设置

      • 确认proxy_pass中的URL是正确的,并且后端服务器能够正确响应。
    4. 预检请求(Preflight)

      • 如果你的API需要发送预flight请求(OPTIONS方法),确保if ($request_method = 'OPTIONS')部分正常工作,即返回204状态码。
    5. 安全策略

      • 确保您的防火墙或代理服务器没有阻止跨域请求。

    解决方案

    1. 检查后端设置

      • 在后端API的配置中检查CORS设置,确保它们与Nginx的配置一致。
    2. 更新Nginx配置

      • 确保proxy_pass_request_headers on;这一行已经启用,它允许将客户端头部传递给后端。
    3. 测试

      • 使用开发者工具检查Access-Control-Allow-Origin等头信息是否在实际响应中存在。
    4. 排查缓存

      • 清除浏览器缓存,或者在开发者工具中使用Network标签的Disable cache选项来临时禁用缓存。
    5. 检查代理问题

      • 确保代理服务器没有限制跨域请求,或者检查代理配置是否有误。
    6. 修改if条件

      • 确保if ($request_method = 'OPTIONS')的条件正确,允许OPTIONS请求。

    记得在调整配置后重启Nginx服务使更改生效。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 6月2日