飘在云端

东西南北,海角天涯

· 前端webServer · · 421次浏览

MyUrls + sub-web 跨域配置

更新:2024-04-10 ,彻底重写文章

最近给前端 sub-web 、myurls 短链后端、后端 subconverter 都更新至上游最新版之后,发现又有跨域问题,折腾一番搞定

经测试,对于 https://u.0z.gs/short 接口及 POST 方法生效,均带有设置的 CORS Header
经测试,对于 任意生成的短链,如 https://u.0z.gs/6JfNauB 均带有设置的 CORS Header
经测试,对于 sub-web 新增的 从 URL 解析,使用 js Fetch API,均带有设置的 CORS Header,可以正确还原短链的订阅 URL

我发现是需要对不同接口单独配置,主要是修改 myurl 反向代理配置,最终配置文件如下

#PROXY-START/
location / {

# 对 短链子域根路径  https://u.0z.gs/ 单独处理
  location =/ {
    #跨域
    add_header 'Access-Control-Allow-Origin' $http_origin always;
    #add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Headers' $http_access_control_request_headers always;
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'Access-Control-Max-Age' 86400 always;
    add_header 'Access-Control-Allow-Methods' 'PUT,GET,POST,DELETE,HEAD,TRACE,OPTIONS,PATCH,CONNECT,COPY,LOCK,MKCOL,MOVE,PROPPATCH,PROPFIND,UNLOCK' always;
     if ($request_method = 'OPTIONS') {
        return 204 always;
       }
       proxy_ssl_server_name on;
    proxy_ssl_name u.0z.gs;
    proxy_pass http://127.0.0.1:8002;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;
  }
  # 对生成的任意短链附加 跨域 CORS
  location ^~ / {
    #跨域
    add_header 'Access-Control-Allow-Origin' $http_origin always;
    #add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Headers' $http_access_control_request_headers always;
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'Access-Control-Max-Age' 86400 always;
    add_header 'Access-Control-Allow-Methods' 'PUT,GET,POST,DELETE,HEAD,TRACE,OPTIONS,PATCH,CONNECT,COPY,LOCK,MKCOL,MOVE,PROPPATCH,PROPFIND,UNLOCK' always;
     if ($request_method = 'OPTIONS') {
        return 204 always;
       }
       proxy_ssl_server_name on;
    proxy_ssl_name u.0z.gs;
    proxy_pass http://127.0.0.1:8002;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;
  }
   # 对未命中的剩余匹配附加 跨域 CORS
    #跨域
    add_header 'Access-Control-Allow-Origin' $http_origin always;
    #add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Headers' $http_access_control_request_headers always;
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'Access-Control-Max-Age' 86400 always;
    add_header 'Access-Control-Allow-Methods' 'PUT,GET,POST,DELETE,HEAD,TRACE,OPTIONS,PATCH,CONNECT,COPY,LOCK,MKCOL,MOVE,PROPPATCH,PROPFIND,UNLOCK' always;
     if ($request_method = 'OPTIONS') {
        return 204 always;
       }

    proxy_ssl_server_name on;
    proxy_ssl_name u.0z.gs;
    proxy_pass http://127.0.0.1:8002;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;
    

    #Set Nginx Cache
    
    
    set $static_file1TCPtDBe 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
        set $static_file1TCPtDBe 1;
        expires 1m;
        }
    if ( $static_file1TCPtDBe = 0 )
    {
    add_header Cache-Control no-cache;
    }
}

    location ~ ^/short  {
    proxy_pass http://127.0.0.1:8002;
  #跨域
    add_header 'Access-Control-Allow-Origin' $http_origin always;
    #add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Headers' $http_access_control_request_headers always;
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'Access-Control-Max-Age' 86400 always;
    add_header 'Access-Control-Allow-Methods' 'PUT,GET,POST,DELETE,HEAD,TRACE,OPTIONS,PATCH,CONNECT,COPY,LOCK,MKCOL,MOVE,PROPPATCH,PROPFIND,UNLOCK' always;
     if ($request_method = 'OPTIONS') {
        return 204 always;
       }
       
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
       
    }

#PROXY-END/
评论 (0条)