PS: 解決客戶端跨域問題谐檀,非服務(wù)器端配置
1. 安裝
sudo brew install nginx
2. 啟動(dòng)
//方式一
sudo nginx
//方式二
sudo brew services start nginx
解決跨域問題
原理:通過合并兩個(gè)網(wǎng)站到同一個(gè)主機(jī)(通過路徑區(qū)分主機(jī)),來避免跨域問題
找到配置文件
/usr/local/etc/nginx/nginx.conf
修改http下面的server節(jié)點(diǎn)
server {
listen 8090; //本地訪問的端口
server_name localhost; //本地訪問的地址
#charset koi8-r;
#access_log logs/host.access.log main;
location / {//服務(wù)器一裁奇,通過 http://localhost:8090/
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
proxy_set_header X-real-ip $remote_addr;
proxy_pass http://localhost:8080;
}
location /api/ { //服務(wù)器二桐猬,通過 http://localhost:8090/api/訪問,api可以改成自定義路勁
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
proxy_set_header X-real-ip $remote_addr;
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://apptest.xxx.net;
}
}
重啟 nginx
sudo nginx -s reload
說明
http://localhost:8090/ //服務(wù)器一
http://localhost:8090/api/ //服務(wù)器二刽肠,可用于程序里調(diào)用api的地址溃肪,一次類推多臺服務(wù)器一樣的配置
可能遇到的問題:
報(bào)錯(cuò):
nginx: [alert] kill(2783, 1) failed (3: No such process)
原因:未啟動(dòng),所以重新啟動(dòng)即可
解決:
sudo nginx
nginx 常用命令
nginx -s reload 重新加載配置
nginx -s reopen 重啟
nginx -s stop 停止
nginx -s quit 退出
nginx -V 查看版本五垮,以及配置文件地址
nginx -v 查看版本
nginx -c filename 指定配置文件
nginx -h 幫助