1、安装nginx
sudo pacman -S nginx
2、启动、测试本地nginx
查看本地nginx情况
sudo systemctl status nginx
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)
启动本地nginx
sudo systemctl start nginx
查看本地nginx情况
sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2019-08-23 13:03:29 CST; 15s ago
Process: 6121 ExecStart=/usr/bin/nginx -g pid /run/nginx.pid; error_log stderr; (code=exited, status=0/SUCCES>
Main PID: 6122 (nginx)
Tasks: 2 (limit: 4915)
Memory: 2.5M
CGroup: /system.slice/nginx.service
├─6122 nginx: master process /usr/bin/nginx -g pid /run/nginx.pid; error_log stderr;
└─6123 nginx: worker process
8月 23 13:03:29 serendipity-zsh systemd[1]: Starting A high performance web server and a reverse proxy server...
8月 23 13:03:29 serendipity-zsh nginx[6121]: 2019/08/23 13:03:29 [warn] 6121#6121: could not build optimal type>
8月 23 13:03:29 serendipity-zsh systemd[1]: Started A high performance web server and a reverse proxy server.
发现有一行红色的提醒,说明一会测试可能会出错。
测试nginx
sudo nginx -t
出现以下错误:
[warn] 6180#6180: could not build optimal types_hash, you should increase either types_hash_max_size: 1024 or types_hash_bucket_size: 64; ignoring types_hash_bucket_size
按照提示进入nginx.conf
sudo vim /etc/nginx/nginx.conf
#在http模块中的server模块上面,加入下面内容
types_hash_max_size 1024;
types_hash_bucket_size 64;
然后重新启动nginx
sudo systemctl restart nginx
测试
sudo nginx -t
然后发现还是会有[warn],但是下面已经出现 syntax is ok 和test is successful,现在就已经可以了
[warn] 6464#6464: could not build optimal types_hash, you should increase either types_hash_max_size: 1024 or types_hash_bucket_size: 64; ignoring types_hash_bucket_size
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
3、使用一个域名配置多个网站/项目
目的
使用一个域名通过访问不同的目录实现多个网站/项目的访问
实现
进入/etc/nginx/conf.d目录
cd /etc/nginx/conf.d
创建一个.conf文件(例如hello.conf)
vim hello.conf
配置nginx的基本参数:
server {
listen 80;
server_name hello.com; # 你的域名
root /data/project-one; #你第一个网站的路径,就是在主目录下网站的路径
location / {
root /data/project-one; # 同上
index index.html; #首页的文件名
}
location /api {
proxy_pass http://127.0.0.1:8081;# 前端代理端口,这个需要开启服务器8081端口的防火墙
}
location /project-two {
alias /data/project-two; # 第二个网站的目录
index index.html; #首页文件名
}
#这段配置是为了把第二个项目的css、js以及图片文件引入,否则会出现第二个项目只有html代码的问题
#这个是根据正则去调整的,需要根据具体的项目结构进行调整
location ~ ^/project-two/.*\.(js|css|png|gif|jpg|mp3|ogg)/$ {
alias /data/project-two/;
}
}
注意:这个首先需要在nginx.conf中把conf.d文件夹下面的配置文件导入
include /etc/nginx/conf.d/*.conf;
这样就可以通过http://hello.com和http://hello.com/project-two分别访问两个项目了,再部署其他项目也是类似的方法
记得修改完nginx配置,重新启动下nginx,最好再测试一下,检查是否有语法错误
#重启nginx
systemctl restart nginx
#测试nginx
nginx -t
4. nginx配置错误记录
systemctl restart nginx
#报错如下:
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
#按照提示进行操作
systemctl status nginx.service
#主要错误
nginx.service: Failed with result 'exit-code'.
#然后看了一下80端口是否被占用
netstat -ntpl | grep 80
#并没有被占用
#所以我想到的就是可能刚才配置的语法出了问题,因为在配置之前是可以正常运行nginx的
#果不其然发现:少了一个空格
在nginx的配置文件中,记得在大括号{}之前一定要有一个空格


