230721_初めてのNginx体験

Haoliang Tang Lv2

部署的流れ:把打包后的folder放到/srv下(这里就是放服务器的目录,虽说放哪里都行只要nginx.conf配置了对应的路径,但最好放/srv下面吧);配置/etc/nginx下的nginx.conf(/etc就是放各种配置文件的);systemctl start nginx开启服务,如果刚改完nginx.conf配置需要重启systemctl restart nginx;然后浏览器输入http://127.0.0.1/orlocalhost(127.0.0.1的域名)应该就能看到了,Nginx默认端口80


先下载Nginx

1
sudo pacman -S nginx

systemctl开启,关闭,查看状态

1
2
3
systemctl status nginx
systemctl start nginx
systemctl stop nginx

nginx后面省略不写的话,默认就是nginx.service

前端项目打包

1
npm run build

生成了一个dist文件夹,把它复制到/srv/http

1
sudo cp -r dist /srv/http

或者

1
sudo cp -r dist ~http/

http可以看做是一个用户,相当于是http的home目录(这里我也不懂)

修改nginx.conf配置文件

终端cd到/etc/nginx后敲xdg-open .就在文件管理器里打开了当前目录

1
sudo gedit /etc/nginx/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

#location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
#}
location / {
# root /srv/http/helloworld;
root /srv/http/dist;
index index.html index.htm;
}

修改location

如果要同时部署前端和后端的话,就要有两个server{}的配置信息

每次修改nginx.conf后要systemctl restart nginx

然后浏览器访问localhost:80,应该就能看到了

  • Title: 230721_初めてのNginx体験
  • Author: Haoliang Tang
  • Created at : 2023-07-21 00:00:00
  • Updated at : 2023-11-02 20:33:32
  • Link: https://hl-tang.github.io/2023/07/21/230721_初めてのNginx体験/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments
On this page
230721_初めてのNginx体験