在location模块里面配置(proxy_pass)
server {
listen 9000
server_name localhost
location / {
root html
index index.html index.htm
proxy_pass http://www.baidu.com
}
//当请求localhost:9000时会被代理到http://www.baidu.com
可能会出现的问题
当更换代理或者移除代理后会因为浏览器的缓存规则导致访问localhost:9000依旧存在代理
解决方法(不配置浏览器缓存)
location / {
root html
index index.html index.htm
add_header Cache-Control no-cache
add_header Pragma no-cache
add_header Expires 0
proxy_pass http://www.baidu.com
}
nginx代理解决跨域问题
跨域(浏览器的同源策略导致,即协议,域名,端口号要一致)
location /api/ {
root html;
index index.html index.htm;
proxy_pass http://localhost:8888;
}