Ubuntu + Strapi:代理传递上的 nginx CORS:跨源请求被阻止:同源策略不允许读取远程资源

Ubuntu + Strapi:代理传递上的 nginx CORS:跨源请求被阻止:同源策略不允许读取远程资源

我已经设置了 nginx,以便将我的 strapi 服务器的 http 请求重定向到 https。但是,使用 https 时我总是遇到 CORS 问题(当我使用 http 时,它会通过)。目前,这是我的 nginx 默认配置:

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

       add_header Access-Control-Allow-Origin *;

        location / {
        proxy_pass http://localhost:1337; # Assuming Strapi runs on port 1337
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;

        }

   }


server {
root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

server_name prj666.ddns.net; # managed by Certbot
add_header Access-Control-Allow-Origin *;

        location / {
     if ($request_method = 'OPTIONS') {
        proxy_pass http://localhost:1337; # Assuming Strapi runs on port 1337
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/prj666.ddns.net/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/prj666.ddns.net/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = prj666.ddns.net) {
        return 301 https://$host$request_uri;
    } # managed by Certbot



        listen 80 ;
        listen [::]:80 ;
    server_name prj666.ddns.net;
    return 404; # managed by Certbot
add_header Access-Control-Allow-Origin *;

 location / {
        proxy_pass http://localhost:1337; # Assuming Strapi runs on port 1337
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;

                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

    ssl_certificate /etc/letsencrypt/live/prj666.ddns.net/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/prj666.ddns.net/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

我已经用多种方式扭曲了它(抱歉格式不对),我尝试在位置/{} 内添加 header 和 proxy_set_header,但它不起作用。

所以我想问一下,还有其他解决办法吗?

另外,这是我的 Strapi 配置:

    name: "strapi::cors",
    config: {
      enabled: true,
      headers: "*",
      origin: [
        "http://localhost:1337",
        "http://localhost:3000",
        "https://ood-system-frontend.vercel.app",
        "http://prj666.ddns.net:1337",
        "https://prj666.ddns.net:1337",
      ],
    },
  },

相关内容