Nginx 解决前后端跨域的问题

  1. #user nobody;
  2. worker_processes 2;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 60;
  20. gzip on;
  21. upstream localhost_fuzai {
  22. server 127.0.0.1:5000 weight=1;
  23. }
  24. server {
  25. listen 80;
  26. server_name 127.0.0.1;
  27. #charset koi8-r;
  28. #access_log logs/host.access.log main;
  29. location / {
  30. root /usr/local/src; #### html文件所在的文件夹
  31. index index.html; ### 前端html文件文件名称
  32. add_header 'Access-Control-Allow-Origin' '*';
  33. }
  34. # 后端接口
  35. location /api/ {
  36. add_header 'Access-Control-Allow-Origin' $http_origin;
  37. add_header 'Access-Control-Allow-Credentials' 'true';
  38. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  39. add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
  40. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
  41. if ($request_method = 'OPTIONS') {
  42. add_header 'Access-Control-Max-Age' 1728000;
  43. add_header 'Content-Type' 'text/plain; charset=utf-8';
  44. add_header 'Content-Length' 0;
  45. return 204;
  46. }
  47. proxy_set_header Host $host;
  48. proxy_set_header X-Real-IP $remote_addr;
  49. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  50. proxy_set_header X-Forwarded-Proto $scheme;
  51. proxy_connect_timeout 5;
  52. ## 如请求 /api/data_list 这个接口后,nginx会将请求自动转发到http://127.0.0.1:5000/data_list这个地址上
  53. proxy_pass http://127.0.0.1:5000/;
  54. #proxy_pass http://localhost_fuzai;
  55. #root html;
  56. #index index.html index.htm;
  57. }
  58. #error_page 404 /404.html;
  59. # redirect server error pages to the static page /50x.html
  60. #
  61. error_page 500 502 503 504 /50x.html;
  62. location = /50x.html {
  63. root html;
  64. }
  65. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  66. #
  67. #location ~ \.php$ {
  68. # proxy_pass http://127.0.0.1;
  69. #}
  70. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  71. #
  72. #location ~ \.php$ {
  73. # root html;
  74. # fastcgi_pass 127.0.0.1:9000;
  75. # fastcgi_index index.php;
  76. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  77. # include fastcgi_params;
  78. #}
  79. # deny access to .htaccess files, if Apache's document root
  80. # concurs with nginx's one
  81. #
  82. #location ~ /\.ht {
  83. # deny all;
  84. #}
  85. }
  86. # another virtual host using mix of IP-, name-, and port-based configuration
  87. #
  88. #server {
  89. # listen 8000;
  90. # listen somename:8080;
  91. # server_name somename alias another.alias;
  92. # location / {
  93. # root html;
  94. # index index.html index.htm;
  95. # }
  96. #}
  97. # HTTPS server
  98. #
  99. #server {
  100. # listen 443 ssl;
  101. # server_name localhost;
  102. # ssl_certificate cert.pem;
  103. # ssl_certificate_key cert.key;
  104. # ssl_session_cache shared:SSL:1m;
  105. # ssl_session_timeout 5m;
  106. # ssl_ciphers HIGH:!aNULL:!MD5;
  107. # ssl_prefer_server_ciphers on;
  108. # location / {
  109. # root html;
  110. # index index.html index.htm;
  111. # }
  112. #}
  113. }