Nginx設置跨域詳解

一、Nginx設置跨域代理

跨域代理是指把請求發送到另外一個域名下的服務器進行處理,再將處理結果返回給客戶端。在前端開發中,我們常常需要在本地開發環境中調用遠端服務器的接口,此時就需要使用到跨域代理。

示例代碼:

location / {
    proxy_pass https://example.com;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

二、Nginx如何設置允許跨域

Nginx可以設置允許跨域請求的HTTP頭字段來解決跨域問題。一般來說,需要設置Access-Control-Allow-Origin頭字段為允許跨域請求的域名。

示例代碼:

add_header 'Access-Control-Allow-Origin' 'http://example.com' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type' always;

三、Nginx設置跨域請求

在使用ajax進行跨域請求時,需要設置請求頭字段X-Requested-With為XMLHttpRequest,以便確保後端可以正確處理跨域請求。

示例代碼:

location /api/ {
    add_header 'Access-Control-Allow-Origin' 'http://example.com' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,X-Requested-With' always;
}

四、Nginx設置跨域不生效

在Nginx配置文件中,如果location匹配成功後,還有其他規則也和匹配上了,那麼不會使用頂層location的跨域規則。此時需要使用break或者return來終止其他規則的匹配。

示例代碼:

location /api/ {
    add_header 'Access-Control-Allow-Origin' 'http://example.com' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,X-Requested-With' always;
    break;
}

五、Nginx設置跨域白名單

在Nginx配置文件中,可以使用if語句判斷請求的來源是否在白名單中,以決定是否允許跨域請求。

示例代碼:

set $allow_cors 0;
if ($http_origin ~* (https?://example\.com(:[0-9]+)?$)) {
    set $allow_cors 1;
}
if ($request_method = 'OPTIONS') {
    set $allow_cors "${allow_cors}opt";
}
if ($allow_cors = "1opt") {
    add_header 'Access-Control-Allow-Origin' "$http_origin" always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,X-Requested-With' always;
    add_header 'Access-Control-Max-Age' 1728000 always;
    add_header 'Content-Length' 0 always;
    return 204;
}

六、Nginx配置跨域代理

Nginx可以配置反向代理來實現跨域請求。將請求發送到本地Nginx,然後Nginx再將請求轉發到目標服務器。

示例代碼:

location /api/ {
    proxy_pass https://example.com;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    add_header 'Access-Control-Allow-Origin' "$http_origin" always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,X-Requested-With' always;
}

七、Nginx設置跨域請求攜帶cookie

在Nginx配置中,需要設置add_header ‘Access-Control-Allow-Credentials’ ‘true’才能允許跨域請求攜帶cookie。

示例代碼:

location /api/ {
    proxy_pass https://example.com;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    add_header 'Access-Control-Allow-Origin' "$http_origin" always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,X-Requested-With' always;
}

八、Nginx跨域怎麼配置

Nginx配置跨域有多種方案,需要綜合考慮業務需求和安全性要求,選擇合適的方案。

示例代碼:

location /api/ {
    if ($http_origin ~* (https?://example\.com(:[0-9]+)?$)) {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,X-Requested-With' always;
        add_header 'Access-Control-Max-Age' 1728000 always;
        add_header 'Content-Length' 0 always;
        if ($request_method = 'OPTIONS') {
            return 204;
        }
        proxy_pass https://example.com;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    if ($http_origin ~* (https?://example2\.com(:[0-9]+)?$)) {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,X-Requested-With' always;
        add_header 'Access-Control-Max-Age' 1728000 always;
        add_header 'Content-Length' 0 always;
        if ($request_method = 'OPTIONS') {
            return 204;
        }
        proxy_pass https://example2.com;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

九、Nginx解決跨域

Nginx解決跨域可以從多個方面入手,如設置允許跨域的HTTP頭字段、設置跨域代理、設置跨域白名單等。

示例代碼:

location /api/ {
    if ($http_origin ~* (https?://example\.com(:[0-9]+)?$)) {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,X-Requested-With' always;
        add_header 'Access-Control-Max-Age' 1728000 always;
        add_header 'Content-Length' 0 always;
        if ($request_method = 'OPTIONS') {
            return 204;
        }
        proxy_pass https://example.com;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

十、總結

本文詳細介紹了Nginx設置跨域的多種方案,包括設置跨域代理、設置允許跨域的HTTP頭字段、設置跨域白名單等。在實際開發中,需要根據業務需求和安全性要求選擇最合適的方案。

原創文章,作者:YWDC,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/147302.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
YWDC的頭像YWDC
上一篇 2024-11-01 14:08
下一篇 2024-11-01 14:08

相關推薦

  • Java如何從Nginx下載文件

    本文將從以下幾個方面詳細介紹如何使用Java從Nginx下載文件。 一、準備工作 在Java中下載文件需要使用到Apache HttpClient庫,這個庫是一個基於Java的HT…

    編程 2025-04-27
  • Linux sync詳解

    一、sync概述 sync是Linux中一個非常重要的命令,它可以將文件系統緩存中的內容,強制寫入磁盤中。在執行sync之前,所有的文件系統更新將不會立即寫入磁盤,而是先緩存在內存…

    編程 2025-04-25
  • 神經網絡代碼詳解

    神經網絡作為一種人工智能技術,被廣泛應用於語音識別、圖像識別、自然語言處理等領域。而神經網絡的模型編寫,離不開代碼。本文將從多個方面詳細闡述神經網絡模型編寫的代碼技術。 一、神經網…

    編程 2025-04-25
  • Python輸入輸出詳解

    一、文件讀寫 Python中文件的讀寫操作是必不可少的基本技能之一。讀寫文件分別使用open()函數中的’r’和’w’參數,讀取文件…

    編程 2025-04-25
  • Python安裝OS庫詳解

    一、OS簡介 OS庫是Python標準庫的一部分,它提供了跨平台的操作系統功能,使得Python可以進行文件操作、進程管理、環境變量讀取等系統級操作。 OS庫中包含了大量的文件和目…

    編程 2025-04-25
  • Linux修改文件名命令詳解

    在Linux系統中,修改文件名是一個很常見的操作。Linux提供了多種方式來修改文件名,這篇文章將介紹Linux修改文件名的詳細操作。 一、mv命令 mv命令是Linux下的常用命…

    編程 2025-04-25
  • C語言貪吃蛇詳解

    一、數據結構和算法 C語言貪吃蛇主要運用了以下數據結構和算法: 1. 鏈表 typedef struct body { int x; int y; struct body *nex…

    編程 2025-04-25
  • 詳解eclipse設置

    一、安裝與基礎設置 1、下載eclipse並進行安裝。 2、打開eclipse,選擇對應的工作空間路徑。 File -> Switch Workspace -> [選擇…

    編程 2025-04-25
  • nginx與apache應用開發詳解

    一、概述 nginx和apache都是常見的web服務器。nginx是一個高性能的反向代理web服務器,將負載均衡和緩存集成在了一起,可以動靜分離。apache是一個可擴展的web…

    編程 2025-04-25
  • Java BigDecimal 精度詳解

    一、基礎概念 Java BigDecimal 是一個用於高精度計算的類。普通的 double 或 float 類型只能精確表示有限的數字,而對於需要高精度計算的場景,BigDeci…

    編程 2025-04-25

發表回復

登錄後才能評論