最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

nginx跨域訪問配置的幾種方法實(shí)現(xiàn)

 更新時(shí)間:2025年12月08日 10:58:38   作者:猩火燎猿  
本文詳細(xì)介紹了Nginx跨域配置方法,包括基本配置、只允許指定域名、攜帶Cookie的跨域、動(dòng)態(tài)設(shè)置允許的Origin、支持不同路徑的跨域控制、靜態(tài)資源跨域以及根據(jù)請(qǐng)求方法細(xì)分CORS策略,感興趣的可以了解一下

一、基本跨域配置

在 nginx 的 location 塊中添加以下內(nèi)容:

location /api/ {
    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
    add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization' always;
 
    # 處理預(yù)檢請(qǐng)求(OPTIONS)
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
    }
 
    # 其他反向代理/靜態(tài)資源配置...
    proxy_pass http://backend-server;
}

說明:

  • Access-Control-Allow-Origin:允許哪些域訪問,* 表示所有域。
  • Access-Control-Allow-Methods:允許的 HTTP 方法。
  • Access-Control-Allow-Headers:允許的請(qǐng)求頭。
  • OPTIONS 方法用于處理 CORS 預(yù)檢請(qǐng)求,返回 204 No Content。

二、只允許指定域名跨域

如果只允許某個(gè)域(如 https://www.example.com)跨域:

add_header 'Access-Control-Allow-Origin' 'https://www.example.com' always; 

三、完整示例

假設(shè)你有一個(gè)前端(端口 8080)和后端(端口 8000),nginx 代理 /api/ 到后端:

server {
    listen 80;
    server_name your.domain.com;
 
    location /api/ {
        proxy_pass http://127.0.0.1:8000;
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
        add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization' always;
 
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }
    }
}

四、配置后重載 nginx

每次修改配置后,記得重載 nginx:

nginx -s reload 

五、注意事項(xiàng)

  • 如果后端也設(shè)置了 CORS,可能會(huì)有沖突,建議只在一個(gè)地方設(shè)置。
  • 如果需要攜帶 cookie,Access-Control-Allow-Origin 不能為 *,必須指定具體域名,并加上 Access-Control-Allow-Credentials: true。

六、支持?jǐn)y帶 Cookie 的跨域配置

如果你需要前端跨域請(qǐng)求時(shí)攜帶 cookie(如登錄態(tài)),CORS 需要特殊配置:

  1. Access-Control-Allow-Origin 不能為 *,必須指定具體的域名。
  2. 需要加上 Access-Control-Allow-Credentials: true。

示例:

location /api/ {
    proxy_pass http://127.0.0.1:8000;
 
    add_header 'Access-Control-Allow-Origin' 'https://www.example.com' always;
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
    add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization' always;
 
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
    }
}

前端請(qǐng)求時(shí)需要設(shè)置:

fetch('https://api.example.com/api/', {
  credentials: 'include', // 允許攜帶 cookie
  // 其他參數(shù)
})

七、根據(jù)請(qǐng)求頭動(dòng)態(tài)設(shè)置允許的 Origin

有時(shí)你希望根據(jù)請(qǐng)求頭 Origin 動(dòng)態(tài)設(shè)置允許的域名,可以用 nginx 的變量:

location /api/ {
    proxy_pass http://127.0.0.1:8000;
 
    if ($http_origin ~* "^https://(www\.example\.com|other\.domain\.com)$") {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
    }
 
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
    add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization' always;
 
    # 處理預(yù)檢請(qǐng)求
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
    }
}

八、常見問題排查

  1. add_header 不生效?

    • 確認(rèn) always 關(guān)鍵字已加上。
    • 確認(rèn)沒有被其他配置覆蓋。
    • 確認(rèn) location 塊沒有被其他 server/location 塊覆蓋。
  2. OPTIONS 請(qǐng)求未返回 204?

    • 檢查 if 語(yǔ)句是否正確。
    • 檢查是否被后端攔截。
  3. 前端報(bào)錯(cuò):CORS header ‘Access-Control-Allow-Origin’ missing?

    • 檢查響應(yīng)頭是否包含該字段。
    • 用瀏覽器開發(fā)者工具 Network 檢查響應(yīng)頭。
  4. 攜帶 cookie 時(shí)跨域失???

    • 確認(rèn) Access-Control-Allow-Origin 不是 *,而是具體域名。
    • 確認(rèn) Access-Control-Allow-Credentials: true 已設(shè)置。
    • 前端 fetch/axios 需配置 credentials。

九、nginx 跨域配置模板(推薦)

可以將以下內(nèi)容作為通用模板:

location /api/ {
    proxy_pass http://backend-server;
 
    # 支持跨域
    set $cors '';
    if ($http_origin ~* '^https://(www\.example\.com|other\.domain\.com)$') {
        set $cors 'true';
    }
 
    if ($cors = 'true') {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
    }
 
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
    add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization' always;
 
    # 預(yù)檢處理
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
    }
}

十、針對(duì)不同路徑/接口做跨域控制

有時(shí)你只希望對(duì)部分接口(如 /api/)開放跨域,對(duì)其他接口(如 /admin/)不開放,可以這樣配置:

server {
    listen 80;
    server_name your.domain.com;
 
    # 只對(duì) /api/ 開放跨域
    location /api/ {
        proxy_pass http://backend-api;
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
        add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization' always;
 
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }
    }
 
    # /admin/ 不開放跨域
    location /admin/ {
        proxy_pass http://backend-admin;
        # 不添加跨域相關(guān) header
    }
}

十一、靜態(tài)資源跨域(如圖片、字體、JS/CSS)

如果你希望靜態(tài)資源(如圖片、字體文件等)可以被其他域引用,需為靜態(tài)資源 location 添加 CORS 響應(yīng)頭:

location /static/ {
    root /var/www/html;
    add_header 'Access-Control-Allow-Origin' '*' always;
    add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always;
    add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept' always;
}

注意:如果你希望字體文件可被第三方引用,必須加上 Access-Control-Allow-Origin,否則會(huì)有跨域問題。

十二、根據(jù)請(qǐng)求方法細(xì)分 CORS 策略

有時(shí)你希望 GET/POST/PUT/DELETE 允許跨域,而 PATCH 不允許,可以這樣寫:

location /api/ {
    proxy_pass http://backend-server;
 
    if ($request_method ~* "GET|POST|PUT|DELETE|OPTIONS") {
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
        add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization' always;
    }
 
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
    }
}

十三、反向代理多后端的跨域配置

如果 nginx 代理多個(gè)后端(如 /api1/、/api2/),每個(gè)后端都需要跨域:

location /api1/ {
    proxy_pass http://backend1;
    add_header 'Access-Control-Allow-Origin' '*' always;
    # ...同上
}
 
location /api2/ {
    proxy_pass http://backend2;
    add_header 'Access-Control-Allow-Origin' '*' always;
    # ...同上
}

十四、nginx 1.7.5 及以上版本的 always 參數(shù)

確保你用的 nginx 版本支持 always 參數(shù),否則有些 header 在 4xx/5xx 狀態(tài)下不會(huì)返回。

十五、安全建議

  1. 生產(chǎn)環(huán)境不要使用 *,要指定具體域名。
  2. 不建議在 / 根路徑全局添加 CORS,容易引發(fā)安全隱患。
  3. 如需支持多域名跨域,推薦用變量和正則動(dòng)態(tài)判斷。
  4. 如果接口涉及敏感數(shù)據(jù),務(wù)必開啟認(rèn)證和 HTTPS。

十六、調(diào)試技巧

  • 用 Chrome/Firefox 開發(fā)者工具的 Network 面板查看 Response Headers,確認(rèn) CORS 頭是否正確返回。
  • 使用 curl -i 命令直接發(fā)起跨域請(qǐng)求,檢查響應(yīng)頭。
  • 檢查 nginx 日志(access.log 和 error.log)排查問題。

十七、完整多場(chǎng)景配置模板

server {
    listen 80;
    server_name your.domain.com;
 
    # 靜態(tài)資源
    location /static/ {
        root /var/www/html;
        add_header 'Access-Control-Allow-Origin' '*' always;
    }
 
    # API1 跨域
    location /api1/ {
        proxy_pass http://backend1;
        add_header 'Access-Control-Allow-Origin' 'https://www.a.com' always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
        add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization' always;
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }
    }
 
    # API2 跨域
    location /api2/ {
        proxy_pass http://backend2;
        add_header 'Access-Control-Allow-Origin' 'https://www.b.com' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
        add_header 'Access-Control-Allow-Headers' 'Origin, Content-Type, Accept, Authorization' always;
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }
    }
}

到此這篇關(guān)于nginx跨域訪問配置的幾種方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)nginx跨域訪問配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Nginx防止直接用IP訪問Web服務(wù)器的設(shè)置方法

    Nginx防止直接用IP訪問Web服務(wù)器的設(shè)置方法

    看了很多Nginx的配置,好像都忽略了ip直接訪問Web的問題,這樣理論上不利于SEO優(yōu)化,所以我們希望可以避免直接用IP訪問網(wǎng)站,而是域名訪問,具體怎么做呢,看下面
    2012-09-09
  • LNMPA遇到504 Gateway time-out錯(cuò)誤的解決方法

    LNMPA遇到504 Gateway time-out錯(cuò)誤的解決方法

    這篇文章主要介紹了LNMPA遇到504 Gateway time-out錯(cuò)誤的解決方法,需要的朋友可以參考下
    2017-07-07
  • Nginx配置中指令root和alias的區(qū)別淺析

    Nginx配置中指令root和alias的區(qū)別淺析

    這篇文章給大家主要介紹了Nginx配置中指令root與alias的區(qū)別,文章介紹的很詳細(xì),詳細(xì)對(duì)大家理解root與alias很有幫助,有需要的朋友們下面來(lái)一起看看吧。
    2016-10-10
  • Linux centos7環(huán)境下Nginx安裝教程

    Linux centos7環(huán)境下Nginx安裝教程

    這篇文章主要為大家詳細(xì)介紹了Linux centos7環(huán)境下Nginx安裝教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • nginx如何根據(jù)用戶真實(shí)IP限源

    nginx如何根據(jù)用戶真實(shí)IP限源

    文章介紹了Nginx限制IP訪問的方式,包括通過remote_addr、X-Real-IP和X-Forwarded-For頭文件獲取用戶真實(shí)IP的方法,并詳細(xì)說明了如何配置Nginx以實(shí)現(xiàn)基于白名單的訪問控制
    2025-11-11
  • Nginx虛擬主機(jī)的配置實(shí)現(xiàn)

    Nginx虛擬主機(jī)的配置實(shí)現(xiàn)

    虛擬主機(jī)指的就是一個(gè)獨(dú)立的站點(diǎn)配置,是nginx默認(rèn)支持的一個(gè)功能,本文主要介紹了Nginx虛擬主機(jī)的配置實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • 小白也可以完成的0基礎(chǔ)部署Nginx服務(wù)

    小白也可以完成的0基礎(chǔ)部署Nginx服務(wù)

    這篇文章主要為大家介紹了0基礎(chǔ)部署Nginx服務(wù)的實(shí)現(xiàn)方式,非常簡(jiǎn)單詳細(xì)零基礎(chǔ)小白跟著做也可以完成,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-03-03
  • nginx ingress的具體使用

    nginx ingress的具體使用

    本文主要介紹了nginx ingress的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • 解析nginx server_name的具體使用

    解析nginx server_name的具體使用

    nginx server_name對(duì)于正確配置虛擬主機(jī)非常重要,本文主要介紹了解析nginx server_name的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • 通過Nginx+Tomcat+Redis實(shí)現(xiàn)持久會(huì)話

    通過Nginx+Tomcat+Redis實(shí)現(xiàn)持久會(huì)話

    這篇文章主要介紹了通過Nginx+Tomcat+Redis實(shí)現(xiàn)持久會(huì)話的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-11-11

最新評(píng)論

岳普湖县| 青浦区| 措美县| 家居| 宿迁市| 屏山县| 凭祥市| 汉川市| 朔州市| 玛多县| 天全县| 进贤县| 鄄城县| 莱州市| 滦平县| 南华县| 宝兴县| 通河县| 稻城县| 沈阳市| 筠连县| 新余市| 同德县| 镇赉县| 丹寨县| 林口县| 汤阴县| 泸西县| 敦化市| 唐河县| 通海县| 萝北县| 蒙山县| 英吉沙县| 竹山县| 镇原县| 太康县| 浪卡子县| 扎赉特旗| 宁阳县| 武穴市|