金戈枸橼酸西地那非片,上海推广seo,平台式网站模板下载地址,国外 网站 欣赏Nginx配置web服务器及部署反向代理配置web服务器location语法部署反向代理代理转发配置web服务器
项目部署到linux上的静态文件代理给Nginx处理。当访问服务器IP时#xff0c;可以自动返回静态文件主页。
主配置文件中server块对应的次配置include /etc/nginx/conf.d/*.conf…
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 Truedebug模式还是为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_rootdist)而对应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;
}