有懂 nginx 的同学,帮我看看 RESTful 项目怎么配置 Cors

我这里用 springboot 写了一个 RESTful Web 应用,还用 Vue 写了一个前端应用
现在我想把 Cors 配置写在 Nginx 中,由于后端是 RESTful 的,原来的 location 配置肯定是不能用了,因为没有 html 文件

location / {
    root html
    index index.html index.htm
}

由于刚学 nginx ,不是很熟悉这玩意,有同学能告诉我该怎么配置吗

1 个赞

你是指 跨域?这个与nginx没什么关系吧,不需要在nginx中配置,而是在你使用的后端语言中配置,输出http响应头即可,如 header('Access-Control-Allow-Origin:*');这个是php的办法,vue不知道如何输出http响应头

@CrossOrigin

我也在找, 找了大半天了 :innocent:

都是说用 apache 的 .htaccess, 我这个以前就是用的它, 现在想换成 nginx, 搞不定

国内的网页 都为了 简单, 介绍的都是用 apache的, 到有用 nginx , 都是抄来抄去的

国外的也 机翻了一些, 好像也都没什么好的问答, 也是说的机车的, 也可能是我 没找到

简单地说 nginx 觉得 htaccess 会拖慢速度,故意不支持,只能全局配置

大部分情况 htaccess 内容放在 nginx.conf 里。

至于怎么配置,网上不是随便都能搜到么

多谢了, 新春快乐!

我刚找到了个, 国内的, 达到当前的目的了, 您给的这个 链接, 我先收着!

拿去

user steiner;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    # default_type  application/octet-stream;
    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;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        # location / {
        #     root   /usr/share/nginx/html;
        #     index  index.html index.htm;
        # }

        location / {
            root /home/steiner/workspace/sayhello/frontend/dist;
            index index.html;
            try_files $uri $uri/ /index.html;
        }

        location /api {
            proxy_pass http://localhost:8082/api;
            add_header Access-Control-Allow-Origin * always;
            add_header Access-Control-Allow-Methods * always;
            add_header Access-Control-Allow-Headers * always;

            if ($request_method = 'OPTIONS') {
               add_header Access-Control-Allow-Origin * always;
               add_header Access-Control-Allow-Methods * always;
               add_header Access-Control-Allow-Headers * always;
               return 204;     
            }
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

其中 localhost / 代理前端的文件 localhost /api 代理后端接口

cors还是建议在springboot里设置,nginx仅作为反向代理使用