欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

nginx 下 解决 vue 跨域问题

程序员文章站 2022-07-10 13:07:26
...
  1. 找到对应的nginx 的 xxx.conf 文件 进行如下配置

nginx 下 解决 vue 跨域问题

server {

    listen 80;

    # For https
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server ipv6only=on;
    # ssl_certificate /etc/nginx/ssl/default.crt;
    # ssl_certificate_key /etc/nginx/ssl/default.key;

    server_name api.lk.cc;
    root /var/www/lk_api/public;
    index index.php index.html index.htm;
    ####error_log /var/www/wwwroot/error/error.log;
    location / {
         try_files $uri $uri/ /index.php$is_args$args;

    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
		//这里开始就是解决 vue 跨域的问题的方法 start
         add_header Access-Control-Allow-Origin *;
         add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS,PUT,DELETE';
         add_header Access-Control-Allow-Headers 'Token,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

        if ($request_method = 'OPTIONS') {
         return 204;
        }
        //这里开始就是解决 vue 跨域的问题的方法 end
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }
}
  1. 从新 载入&启动 nginx
systemctl reload nginx     systemctl restart nginx
相关标签: vue nginx 跨域