Django_Vue3_ElementUI_Release_004_使用nginx部署

1. nginx安装配置

1.1 下载nginx

Download nginx

1.2 测试一下

1.3 进入nginx用命令操作

2. 部署

2.1 前端部署

2.1.1 修改nginx监听配置

...conf/nginx.conf

bash 复制代码
http {
  ... # 这里不进行修改
  server {
    listen 8010; # 监听 80 端口
    server_name 192.168.10.24; # 输入服务器 ip,我这里为内网 ip
    
    location / {
      root html;
      index index.html index.htm; # 这里默认为此配置,表示当有人访问 服务器 80 端口的 / 根目录,那么 Nginx 将在 html 文件夹中寻找 index.html, index.htm 文件进行展示,也可以根据自己实际情况进行修改
      # 如果 vue 的路由模式是 history,一定要加上下面这句话
      try_files $uri $uri/ /index.html;
    }
  }
}

2.1.2 修改vue axios监听配置

和nginx的保持一致,因为之前是vue直接访问后端的,现在改成了nginx转发

2.1.3 修改完成后打包

npm run build

2.1.4 将dist文件夹中的内容拷贝到nginx的html目录中

2.1.5 浏览器访问测试

以上前端配置好了,但是不能访问后端

2.2 后端部署

2.2.1 nginx需要如下配置

c 复制代码
		location /api/ {
			add_header Access-Control-Allow-Origin *;
			proxy_set_header Host $http_host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header REMOTE-HOST $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			# Nginx 监听到请求 api后,将请求转发给 localhost的8000端口,
			# 因为 Django后端也要部署到这台服务器上,所以是 localhost,
			# 这样可以减少用户请求次数,加快网站访问速度。(我实测速度是有一定提升)
			proxy_pass http://127.0.0.1:8000;  
		  }

以上配置文件中的ip地址和端口需要和django中保持一致

2.2.2 如果有静态文件,需要如下配置

c 复制代码
		location /static/ {  
			# 这里为你的需要访问文件的访问路径,
			# 我的文件访问路径是 http://192.168.50.10/static/papers/XXX.pdf,
			# 我的文件存储在了 static/papers/XXX.pdf,并且一同复制到了 html 文件夹中。
			alias D:/Web/nginx-1.24.0/html/static/;  
			# 这里为服务器中 html 内,你的文件的存储路径。
			try_files $uri $uri/;
		  }

2.2.3 settings.py中做如下配置

3. 测试

启动nginx并访问

4. 修复django admin

在 服务器的 Django 的 settings.py 中,新增以下配置

c 复制代码
STATIC_ROOT = "D:/Web/nginx-1.24.0/html/static/static/" # 这里为你的服务器中 Nginx 的路径,应在 html 文件夹下的 static 文件夹,但是我的static 文件夹存了论文不为空,因此我在 static 文件夹中新建了文件夹 static

随后在终端中执行

c 复制代码
python manage.py collectstatic  # 将 admin 样式复制到指定目录

将 html/static/static 文件夹的 admin 文件夹复制到 html/static 文件夹中。

随后在 Nginx 的 conf/nginx.conf 文件中进行如下配置。

c 复制代码
location /api/ {
  ... # 以上配置内容
}
location /admin/ {
  add_header Access-Control-Allow-Origin *;
  proxy_set_header Host $http_host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header REMOTE-HOST $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://localhost:8000;
}
location /static/admin/ {
  alias D:/Web/nginx-1.24.0/html/static/admin/;  # 这里为 admin文件夹所在位置
  try_files $uri $uri/;
}
location /static/ {
  ... # 以上配置内容
}

以上内容参考

https://www.cnblogs.com/kyguo1997/p/17884479.html

相关推荐
c8i3 小时前
drf初步梳理
python·django
c8i1 天前
django中的FBV 和 CBV
python·django
雨落Liy2 天前
Nginx 从入门到进阶:反向代理、负载均衡与高性能实战指南
运维·nginx·负载均衡
Yyyy4822 天前
Nginx负载均衡集群实验步骤
运维·nginx·负载均衡
百锦再2 天前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame
qq_264220893 天前
Nginx优化与 SSL/TLS配置
运维·nginx
计算机编程小央姐3 天前
跟上大数据时代步伐:食物营养数据可视化分析系统技术前沿解析
大数据·hadoop·信息可视化·spark·django·课程设计·食物
诗句藏于尽头3 天前
Django模型与数据库表映射的两种方式
数据库·python·django
matlab的学徒3 天前
Web与Nginx网站服务(改)
linux·运维·前端·nginx·tomcat
邂逅星河浪漫3 天前
【Docker+Nginx+Ollama】前后端分离式项目部署(传统打包方式)
java·nginx·docker·部署