thx / rap2-dolores

MIT License
610 stars 255 forks source link

Nginx关于后端接口跨域配置问题 #19

Closed juliana-zhu closed 6 years ago

juliana-zhu commented 6 years ago

前后端代码clone编译之后localhost访问并没有问题,换成ip访问会有跨域问题,所以使用Nginx代理。 修改了config.prod.js

module.exports = {
  serve: '/apis',
  keys: ['some secret hurr'],
  session: {
    key: 'koa:sess'
  }
}

Nginx的配置如下


worker_processes  1;

error_log  logs/error.log debug;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on; 
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  192.168.80.1;
        root    /rap2/rap2-dolores/build;
        location / {
          try_files $uri /index.html;
        }       
        location /apis {
            rewrite  ^.+apis/?(.*)$ /$1 break;
            proxy_pass   http://localhost:8080;
        }       
    } 

}

后端接口代理到http://localhost:8080 然鹅访问时却是这样的 _20180212123551 第一个接口成功,第二个404,第三个405 有时刷新还会是这样的 qq 20180212124231 第一个接口都404 问:官方有Nginx的标准配置方案么?

juliana-zhu commented 6 years ago

解决了,最后发现是8080端口的问题,换了一个后端的端口,问题解决了