一、Nginx代理跨域配置
在開發中,由於瀏覽器同源策略的限制,會存在跨域問題。Nginx是一個高性能的Web伺服器,它有著強大的代理功能,可以通過Nginx代理跨域來解決這個問題。
下面是配置示例:
location /api/ { proxy_pass http://example.com/; proxy_set_header Host $host; proxy_set_header Origin $host; proxy_set_header Referer $host; }
其中,proxy_pass指定代理的目標地址,而proxy_set_header則設置一些頭部信息。Origin和Referer是防止瀏覽器CSRF攻擊的必要頭部信息,需要加上。
二、Nginx配置代理解決跨域問題
可以通過修改Nginx配置文件來解決跨域問題。
下面是配置示例:
location / { add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE'; if ($request_method = 'OPTIONS') { return 204; } }
其中,Access-Control-Allow-Origin指定允許跨域的域名,可以使用通配符*表示允許所有域名。Access-Control-Allow-Headers設置允許跨域的頭部信息。Access-Control-Allow-Methods指定允許的請求方法。當請求方法為OPTIONS時,直接返回204,表示允許請求。
三、Nginx代理跨域/api
Nginx代理跨域/api通常用於代理RESTful API。
下面是配置示例:
location /api/ { proxy_pass http://example.com/; proxy_set_header Host $host; proxy_set_header Origin $host; proxy_set_header Referer $host; }
四、Nginx代理資料庫
在實際開發中,有時需要通過Nginx代理訪問資料庫。
下面是配置示例:
location / { proxy_pass http://localhost:3000/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; }
其中,proxy_pass指定代理的目標地址(此處為本地的3000埠)。proxy_http_version指定HTTP協議版本(1.1)。proxy_set_header Upgrade和proxy_set_header Connection是為了支持WebSocket。其他設置與前面相似。
五、Nginx代理跨越
除了代理跨域,Nginx還可以用來代理跨越。
下面是配置示例:
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,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization'; # # 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,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization'; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range'; } 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,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization'; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range'; } }
此處採用了常用的RESTful風格,並添加了Access-Control-Allow頭部信息,允許跨域訪問。
六、Nginx代理模式有哪些
Nginx支持多種代理模式,包括反向代理、負載均衡和高可用等。
反向代理:指的是將客戶端請求轉發到多個伺服器上。
負載均衡:指的是將客戶端請求平均地分配到多個伺服器上,以提高系統吞吐量和性能。
高可用:指的是將多個伺服器配置在一起,以保證在某個伺服器故障時可以無縫地切換到其它可用的伺服器上,以提供可靠的服務。
七、Nginx代理區域網yum
變相實現yum代理緩存。yum在區域網中的使用非常普遍。通過Nginx來代理yum,可以在本地鏡像伺服器上建立一個yum代理緩存。當客戶端需要軟體包時,可以從這個本地代理緩存中快速地獲取。
下面是配置示例:
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=yum_cache:10m inactive=60m; server { listen 80; server_name yum.localdemo.com; root /usr/share/nginx/html; location / { index index.html; } location /repos { proxy_pass http://repo.localdemo.com/; proxy_set_header Host $host; proxy_cache yum_cache; proxy_cache_valid 200 15m; proxy_cache_valid 404 1m; add_header X-Cache-Status $upstream_cache_status; } }
其中,proxy_cache_path用於設置Nginx的緩存路徑,方便快速地響應客戶端請求。server_name指定本地鏡像伺服器的地址。location /repos指定緩存的yum軟體包所在目錄。
八、Nginx代理
Nginx作為高性能的Web伺服器,在代理方面有著不可替代的優勢。除了上面介紹的代理模式外,還可以通過反向代理來實現訪問外部資源。
示例:
upstream backend { server 192.168.1.1 weight=10; server 192.168.1.2 weight=10; } server { listen 80; server_name frontend.localdemo.com; location / { root /usr/share/nginx/html/; try_files $uri /index.html; } location /api { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
其中,upstream backend定義了後端伺服器的地址和權重。location /api指定了代理的目標地址,並設置了一些頭部信息。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/219620.html