当前位置: 首页 > news >正文

Nginx配置web服务器及部署反向代理

Nginx配置web服务器及部署反向代理

      • 配置web服务器
        • location语法
      • 部署反向代理
        • 代理转发

配置web服务器

项目部署到linux上的静态文件代理给Nginx处理。当访问服务器IP时,可以自动返回静态文件主页。

主配置文件中server块对应的次配置include /etc/nginx/conf.d/*.conf,也就是/etc/nginx/conf.d/目录下的配置文件,可以在该目录下新增一个autotpsite.conf配置文件,执行命令:vim /etc/nginx/conf.d/autotpsite.conf

注:主配置文件中server块默认监听端口为80,若次配置文件中server块监听端口与主配置文件中相同,会产生冲突

server {listen       8080;listen       [::]:8080;server_name  _;# root         /data/project/autotpsite/dist;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}       
}

其中server_name下一行中的root /data/project/autotpsite/dist;这里配置的静态文件根目录,就是项目静态文件所在的目录

访问 http://127.0.0.1:8080/index.html 对应的文件目录是/data/project/autotpsite/dist/index.html

但是一般情况下,不这样进行配置,而是放到location块

server {listen       8080;listen       [::]:8080;server_name  _;# root         /data/project/autotpsite/dist;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}location / {root   /data/project/autotpsite/dist;  }      
}

location语法

相对匹配:

location /path/ {root   /data/software/static;  
}

文件路径等于 root + location/data/software/static 拼接 /path/,如访问 http://ip/path/ 对应的文件目录/data/software/static/path/

举例:

location / {root   /data/project/autotpsite/dist;  
}

访问 http://127.0.0.1:8080/index.html 对应的文件目录是/data/project/autotpsite/dist/index.html

绝对匹配:

location /path/ { alias   /data/software/static/; 
}

文件路径等于alias对应目录,与location无关, 目录必须以 / 结尾,如访问 http://ip/path/ 对应的文件目录 /data/software/static/

举例:

location /static/ {alias   /data/project/autotpsite/dist/;  
}

访问 http://127.0.0.1:8080/static/index.html 对应的文件目录是/data/project/autotpsite/dist/index.html,也就是说,访问路径里需要加上路由static才能够访问

alias 与 root 是二选一的关系 ,不能并存

location指令说明:

功能:用于匹配URL,语法如下:

1= :用于不含正则表达式的 URL 前,要求请求字符串与 URL 严格匹配,如果匹配
成功,就停止继续向下搜索并立即处理该请求。
2、~:用于表示 URL 包含正则表达式,并且区分大小写。
3、~*:用于表示 URL 包含正则表达式,并且不区分大小写。
4、^~:用于不含正则表达式的 URL 前,要求 Nginx 服务器找到标识 URL 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。

注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识

部署反向代理

反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器 IP 地址。

在这里插入图片描述

访问服务器的时候,可以发现只能返回静态文件,而静态文件中的Ajax请求全部失效了,接口并没有返回任何内容

原因就是 web 服务没有代理app服务,app服务是由uWSGI代理的。所以我们要做的就是将发送app服务的请求转给 uWSGI就可以了。

在Django项目的settings.py配置文件中DEBUG = True,debug模式还是为TRUE,是通过urls.py文件中配置的静态文件目录去访问静态文件,这里需要将静态文件的访问交由Nginx处理

from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path,include# from sqtp import urls
urlpatterns = [path('admin/', admin.site.urls),path('api/',include('sqtp.urls')),
]+ static("/",document_root='dist')

而对应api/这个接口,也交由Nginx配置反向代理,通过Nginx再转发给uWSGI代理

代理转发

autotpsite.conf配置文件进行编辑,在Nginx中配置proxy_pass代理转发

这里只有一个服务器,也就是说反向代理服务器和目标服务器是同一个服务器,所以配置proxy_pass代理转发为127.0.0.1,而Django项目的端口为8888,所以proxy_pass http://127.0.0.1:8888

server {listen       8080;listen       [::]:8080;server_name  _;# root         /data/project/autotpsite/dist;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}location / {root   /data/project/autotpsite/dist;  }#location /api/ {#    proxy_pass  http://127.0.0.1:8888;#}location /api/ {include       uwsgi_params;uwsgi_pass     127.0.0.1:8888; # 此方式需要 uwsgi采用socket连接方式}
}

Django项目的app服务是由uWSGI服务器代理的,这里可以不使用http协议,而是采用socket连接方式,这里uwsgi_pass代理转发对应的是一个二进制的socket协议

更新uWSGI服务配置

[uwsgi]
chdir =./
// 项目根目录,配置文件处于项目根目录,因此设置为相对路径即可,复用性更高
module = autotpsite.wsgi:application
// 指定wsgi模块下的application对象
socket = 0.0.0.0:8888
//Nginx使用uwsgi_pass做方向代理时 需要设置成socket
master = true
// 主进程
pidfile = uwsgi8888.pid
// pid 文件,用于脚本启动,停止该进程
daemonize = uwsgi_server.log
// 日志文件
enable-threads = true
// 新增配置--允许多线程
buffer-size = 40960
// 设置请求头最大字节数,用于socket模式

location这里也可以使用正则的方式匹配URL,同时支持api/jira/两个url

location ~/(api/|jira/) {include       uwsgi_params;uwsgi_pass     127.0.0.1:8888;
}
http://www.lryc.cn/news/21372.html

相关文章:

  • mvvm和mvc
  • JavaScript while 循环
  • CMU15-445 Project.0总结
  • 计算机网络题库---错题本
  • 【react】react创建项目与引入AntD组件库:
  • hook与mixin
  • 【C语言】自定义类型
  • 没有上司的舞会(C++,树形DP)
  • 【java基础】static和final关键字的作用及其用法详解
  • #集成学习#:bagging、boosting、stacking和blending
  • NCRE计算机等级考试Python真题(一)
  • C#协变逆变
  • 算法设计与分析期末考试复习(四)
  • qsort函数排序数据 and 模拟实现qosrt函数(详解)
  • Mysql视图,存储过程,触发器,函数以及Mysql架构
  • 什么是线程死锁?如何解决死锁问题
  • C语言几种判断语句简述
  • 【python学习笔记】:SQL常用脚本(二)
  • 【Linux】进程地址空间
  • Qt音视频开发17-vlc内核回调拿图片进行绘制
  • 安装配置DHCP
  • MarkDown中写UML图的方法
  • Axure8设计—动态仪表盘
  • 【C++】类和对象的六个默认成员函数
  • 4、算法MATLAB---认识矩阵
  • vue3+rust个人博客建站日记2-确定需求
  • Linux安装云原生网关Kong/KongA
  • Vue学习笔记(2)
  • 2023年三月份图形化四级打卡试题
  • Python操作Excel