下面的配置是nginx.conf的示例
nginx反向代理 就是說把跨域的url通過本地代理的方式,變成同域的請求,如此來解決跨域問題
該配置下 通過http://localhost/html5/路徑下的文件去請求http://localhost/request/的相關接口就相當於去請求http://localhost:8888/login/相關的接口
#user root owner; #在mac中獲取權限時需要將注釋去掉
worker_processes 2; #啟動進程,通常設置成和cpu的數量相等
events {
worker_connections 1024; #單個后台worker process進程的最大並發鏈接數
}
http {
include mime.types; #設定mime類型,類型由mime.type文件定義
default_type application/octet-stream;
#sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy)來輸出文件,對於普通應用,
#必須設為 on,如果用來進行下載等應用磁盤IO重負載應用,可設置為 off,以平衡磁盤與網絡I/O處理速度,降低系統的uptime.
sendfile on;
keepalive_timeout 65; #連接超時時間
server {
listen 80; #偵聽80端口
server_name localhost; # 定義使用www.xx.com訪問
charset utf-8;
location / {
root html;
add_header Cache-Control no-store;
add_header 'Access-Control-Allow-Origin' '*';
index index.html index.htm;
}
location /request/ {
root /;
proxy_set_header Host $host; #請求主機頭字段,否則為服務器名稱。
proxy_headers_hash_max_size 1024; #存放http報文頭的哈希表容量上限,默認為512個字符
proxy_headers_hash_bucket_size 128; #設置頭部哈希表大小 默認為64
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
proxy_set_header Accept-Encoding "";
proxy_pass http://localhost:8888/login/; #請求替換地址 例如要請求http://localhost:8888/login/ 也就是請求http://localhost/request/
}
location ~ ^/html5/ {
rewrite /html5(.*)$ $1 break; #該指令根據表達式來重定向URI, 路徑中存在/html5/的
expires -1; #緩存
root D:\html5; #前端靜態文件物理路徑 通過http://localhost/html5/來訪問D盤html5下的文件夾
}
}
}