nginx 前后端分離 代理轉發,解決跨域問題


場景

  1. 適用於公司有前端,項目采用前后端分離。類似於我們 后端 springboot 提供接口,前端專門寫html調用相應的接口,解決跨域問題

配置說明

worker_processes  1;



events {
    worker_connections  10240;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    client_max_body_size 200M;
    client_header_buffer_size 8k;
    large_client_header_buffers 8 16k;

    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  on;

    sendfile        on;

    keepalive_timeout  300;

    gzip  on;
    gzip_http_version 1.0;
    gzip_disable "MSIE [1-6].";
    gzip_types text/plain application/x-javascript text/css text/javascript;


    server {
        listen       80;
        server_name  localhost;
        client_header_buffer_size 8k;
        large_client_header_buffers 8 16k;


        root   /usr/share/nginx/html;

          location / {

              # 把跟路徑下的請求轉發給前端工具鏈(如gulp,webstorm,anywhere)打開的開發服務器
              # 如果是產品環境,則使用root等指令配置為靜態文件服務器
              # proxy_pass http://localhost:80;

              #proxy_redirect default;
          }

      location /management/ {
          # 把 /api 路徑下的請求轉發給真正的后端服務器
        proxy_pass http://192.168.199.131:8090/management/;
          proxy_cookie_path  /management/ /;
        proxy_set_header   Host    $host;
        proxy_set_header Cookie $http_cookie;
        proxy_set_header   Remote_Addr    $remote_addr;
        proxy_set_header   X-Real-IP    $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

          client_max_body_size 200m;

          proxy_connect_timeout 18000;

          proxy_send_timeout 18000;

          proxy_read_timeout 18000;
        }


      location /agents/ {
        proxy_pass http://192.168.199.131:8092/;
          proxy_cookie_path  /agents/ /;
        proxy_set_header   Host    $host;
        proxy_set_header Cookie $http_cookie;
        proxy_set_header   Remote_Addr    $remote_addr;
        proxy_set_header   X-Real-IP    $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
       
        client_max_body_size 200m;

        proxy_connect_timeout 18000;

        proxy_send_timeout 18000;

        proxy_read_timeout 18000;
      }

    }
}

重點說明 location 的配置 。

比如前端html請求地址 http://localhost:80/index.html

前端調用接口的地址為 http://localhost:80/api 其實api並不在改域下,在http://192.168.199.111:8888/ 下 則需要解決2個問題 前端ajax跨域與接口轉發到相應位置

** 着重看 1 2 3 **

     location /api/ {  // 1 
    proxy_pass http://192.168.199.111:8888/; // 2 
      proxy_cookie_path  /api/ /; // 3 
    proxy_set_header   Host    $host;
    proxy_set_header Cookie $http_cookie; // 發送cookie 解決 session 一致性問題
    proxy_set_header   Remote_Addr    $remote_addr;
    proxy_set_header   X-Real-IP    $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    client_max_body_size 200m;

    proxy_connect_timeout 18000;

    proxy_send_timeout 18000;

    proxy_read_timeout 18000;
  }

}
**如感覺文章對你有所幫助,可以關注微信公眾號【五彩的顏色】鼓勵一下**
![](https://img2018.cnblogs.com/blog/1821244/201910/1821244-20191030103551015-6251218.jpg)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM