nginx 代理之修改header 的HOST,實現代理轉代理


 

現有一個需求,需要從nginx跳轉到k8s的traefik代理上,從而實現服務的訪問,用於測試。直接修改proxy_set_header的HOST,修改為traefuk代理的域名,proxy_pass是traefik的代理地址.

 

 

server {
listen 80;
server_name kbe.get.com;
location /api {
#auth_basic "it's protected";
#auth_basic_user_file /data/.htpassd;
proxy_pass http://10.1.2.9;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host feature-distinct-kube.app.com;
}
}

 

 

解決跨域問題:

在location段添加如下內容:

  if ($request_method = 'OPTIONS') {

     add_header 'Access-Control-Allow-Origin' '*';

     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

   # # Custom headers and headers various browsers *should* be OK with but aren't

        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

       # # Tell client that this pre-flight info is valid for 20 days

       add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8';

       add_header 'Content-Length' 0;

       return 204; }

   if ($request_method = 'POST') {

     add_header 'Access-Control-Allow-Origin' '*';

     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

     add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; }

  if ($request_method = 'GET') {

     add_header 'Access-Control-Allow-Origin' '*';

     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

     add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; }


免責聲明!

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



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