【问题标题】:Nginx and saleor configuration problems (Blocked by CORS policy)Nginx 和 saleor 配置问题(被 CORS 策略阻止)
【发布时间】:2020-11-29 17:24:09
【问题描述】:

我是 saleor.io 的新手,它是一个电子商务应用程序和 nginx 的开源平台。这个卖家有三个模块:

  1. Saleor 核心(Django 项目,通过 graphql api 与 saleor store 和 saleor dashboard 通信,连接到 postgresql,在 192.168.0.102/grapghql/ 上运行)
  2. Saleor 仪表板(Node 项目,包含 javascript,键入脚本文件,在 192.168.0.102:70 上运行)
  3. Saleor Store(Node 项目,包含 javascript,键入脚本文件)

我在 nginx 上运行这三个模块,UI 完美显示每个模块。但是当我尝试通过仪表板登录时,我收到了这个 CORS 错误:

这是我的 saleor core 的 nginx 配置文件:

upstream django {
     server unix:///home/umair/PythonDjangoProjects/GitSaleor/mysite.sock fail_timeout=9000; # for a file socket
   # server 192.168.0.102:80; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name 192.168.0.102; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
         alias /home/umair/PythonDjangoProjects/GitSaleor/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/umair/PythonDjangoProjects/GitSaleor/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {

     uwsgi_pass  django;
        include     /home/umair/PythonDjangoProjects/GitSaleor/uwsgi_params;  # the uwsgi_params file you installed
 

} }

这是我用于销售或仪表板的 nginx 配置文件:

server {
    listen 70;
    listen [::]:70;
    root /var/www/html/dashboard;
    index index.html;
    server_name 192.168.0.102;
    location / {
try_files $uri $uri/ /index.html?$args;
    }

}

我该如何解决这个错误?我是否遗漏了配置中的任何内容。

【问题讨论】:

    标签: nginx cors saleor


    【解决方案1】:

    我认为你应该尝试把

    if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    }
    if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    }
    if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    }
    

    在您的销售或核心位置块内。

    更多信息在这里: https://enable-cors.org/server_nginx.html

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 2019-09-15
      • 2021-04-15
      • 1970-01-01
      • 2020-06-29
      • 2019-11-01
      • 2023-01-14
      • 1970-01-01
      • 2020-01-15
      相关资源
      最近更新 更多