作者简介:大家好,我是撸代码的羊驼,前阿里巴巴架构师,现某互联网公司CTO
联系v:sulny_ann(17362204968),加我进群,大家一起学习,一起进步,一起对抗互联网寒冬
# 序言
使用Nginx反向代理,可以解决跨域无权和Session丢失的问题,十分方便。下面我们以前后端分离为案例,展开Nginx的使用教程。
# 配置和启动Nginx
下载地址
Nginx下载传送门:http://nginx.org/en/download.html
注意事项:下载之后,记得解压到全英文路径,避免中文路径导致Nginx启动失败。
修改配置
打开nginx.conf ,清空配置项,然后将下面的配置信息原封不动拷贝进去:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#前端页面服务器
server {
#监听端口和域名
listen 7000;
server_name localhost;
#添加头部信息
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#添加拦截路径和代理地址
location /api/ {
proxy_pass http://localhost:8080/; #注意:使用代理地址时末尾记得加上斜杠"/"。
}
#添加拦截路径和根目录
location / {
root html/hehe; #注意:使用"/"拦截全路径的时候记得放在最后。
index index.html index.htm; #index表示首页
}
}
}
快速启动
在Windows 环境中: