配置前端项目和反向代理后端接口
前端bootstrap页面-blog{js,css,xx.html}
后端apache部署的django接口
server {
listen 8001; #自定义端口 ,注意访问时候带上端口,如果是80默认端口则不需要带(localhost:8001)
server_name localhost ipberryha.com; #监听地址,域名或ip,可以多个,中间空格
#charset koi8-r;
#access_log logs/host.access.log main;
upstream backend{
server 127.0.0.1:7878; #这里写两个并且没权重,那就默认轮询,abab....,属于负载均衡的一种用法,有weight按照weight比例使用
server 192.168.10.121:8080;
server 192.168.10.121:3333 backup; #热备 带上backup如果你有2台服务器,当一台服务器发生事故时,才启用第二台服务器给提供服务
ip_hash #nginx会让相同的客户端ip请求相同的服务器
}
location / {
root html/blog; //这是windows下的前端项目根目录(里面是html,js,css等)
#输入网址(server_name:port)后,默认的访问页面
index index.html index.htm;
}
#反向代理转发后端接口(可以先不配置)
# location /api ,注意location结尾有/会导致404,导致api/后面的匹配不到
# 没有“/”结尾时,location/api/def 可以匹配 /api/defghi 请求,也可以匹配 /api/def/ghi 等
# 而有“/”结尾时,location/api/def/ 不能匹配 /api/defghi 请求,只能匹配 /api/def/anything 这样的请求
# proxr_pass 后面/代表绝对路径,有则 Nginx 不会把 location 中匹配的路径部分加入代理 uri,没有则带上/api
# 所以我这里设置location后面没有/,而proxy_pass后面带/,来匹配所有后端路径,同时舍弃后端url不包括的/api
# 例如localhost:81/api/index -->localhost:82/index #这里前端访问有/api,跳转到后端无/api
# 例如 localhost:81/api/v1/users ->localhost:82/v1/users
location /api {
proxy_pass http://backend; 请求转向backend定义的服务器列表(这里就像写个变量名,也可以直接写http//127.0.0.1:7878/;)location里协议,如"http"或"https"必须
#deny x.x.x.x; #拒绝的ip 根据需要来决定写不写
#allow x.x.x.x; #允许的ip
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
参考链接:菜鸟教程https://www.runoob.com/w3cnote/nginx-setup-intro.html
nginx 之 proxy_pass详解https://blog.csdn.net/u010433704/article/details/99945557
Nginx之负载均衡upstream模块简介和使用