Nginx跨域问题解决

  • 背景:云服务器上面部署了后端和前端,前端开发在本地启一个web访问页面,然后访问云服务的后端,然后出现问题

  • 问题:Access to XMLHttpRequest at 'http://192.168.10.100:8070/auth/login' from origin 'http://localhost:9090' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

  • 解决方法:查了一下说是跨域的问题 修改nginx配置

  • 原有配置

bash 复制代码
server {
    listen 6606;
    server_name maintain;
    location / {
        root /opt/lumiankexue/web/bridge/dist;
        try_files $uri $uri/ /index.html;
        index index.html index.htm;
    }
    location /prod-api/ {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://192.168.0.44:8070/;
        client_max_body_size 200M;
        client_body_buffer_size 200M;
    }
    # 禁止访问/actuator路径
    if ($request_uri ~ "/actuator") {
        return 403;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }
}
  • 新增配置
bash 复制代码
        # 允许跨域请求的配置
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' '*' always;
        add_header 'Access-Control-Allow-Headers' '*' always;
        # 处理预检请求(OPTIONS方法)
        if ($request_method = 'OPTIONS') {
            return 204;
        }
bash 复制代码
#新增标红的这些内容,*号代表允许所有
1. add_header 'Access-Control-Allow-Origin' '*' always;:
#这个配置指定了允许访问资源的域。在这种情况下,'*' 表示允许所有域名访问资源,这意味着任何网站都可以访问您的资源。这可能会存在安全风险,因为允许所有域名访问资源可能会导致跨站请求伪造(CSRF)攻击等安全问题。
2. add_header 'Access-Control-Allow-Methods' '*' always;:
#这个配置指定了允许的请求方法。与上面提到的配置不同,'*' 在这里指示允许所有的请求方法。这将允许发起任何类型的请求(GET、POST、PUT、DELETE 等)来访问资源。
3. add_header 'Access-Control-Allow-Headers' '*' always;:
#这个配置指定了允许的请求头。类似地,'*' 表示允许所有的请求头。这将允许浏览器在跨域请求中发送任何类型的请求头信息。
  • 增加之后的配置
bash 复制代码
server {
    listen 6606;
    server_name maintain;
    location / {
        root /opt/lumiankexue/web/bridge/dist;
        try_files $uri $uri/ /index.html;
        index index.html index.htm;
    }
    location /prod-api/ {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # 允许跨域请求的配置
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' '*' always;
        add_header 'Access-Control-Allow-Headers' '*' always;
        # 处理预检请求(OPTIONS方法)
        if ($request_method = 'OPTIONS') {
            return 204;
        }
        proxy_pass http://192.168.0.44:8070/;
        client_max_body_size 200M;
        client_body_buffer_size 200M;
    }
    # 禁止访问/actuator路径
    if ($request_uri ~ "/actuator") {
        return 403;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }
}

然后问题解决

相关推荐
ping某2 天前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
A小辣椒2 天前
TShark:Wireshark CLI 功能
linux
A小辣椒2 天前
TShark:基础知识
linux
AlfredZhao2 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao3 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334663 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪3 天前
linux 拷贝文件或目录到指定的位置
linux
摇滚侠4 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
bush44 天前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行5204 天前
Linux 11 动态监控指令top
linux