centos7 安装 nginx (实操版)

145 阅读2分钟
  1. Nginx 在 CentOS 软件包附带的标准存储库中不可用,安装EPEL 存储库
sudo yum install -y epel-release

  1. 安装nginx
sudo yum install nginx
  1. 开启nginx服务,查看状态,/etc/nginx/nginx.conf
sudo systemctl start nginx
sudo systemctl status nginx
  1. 如果有firewall, 运行下面代码开启http和https
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
  1. 云服务器的话,要在安全组上把80端口打开

1.jpg

  1. 验证服务是否正常开启,能看到临时的页面就好了
http://server_domain_name_or_IP/
  1. 系统重启时启动
sudo systemctl enable nginx

返回

Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

系统启动时会调用/usr/lib/systemd/system/nginx.service代码

  1. 修改/etc/nginx/nginx.conf内容
sudo systemctl restart nginx
sudo systemctl status nginx
  1. 多台服务器,可以实现负载均衡 backup(备份) 三台服务器backend1.example.com,backend2.example.com,192.0.0.1,组成一个组叫backend 访问/的时候可以负载均衡
http {
    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
        server 192.0.0.1 backup;
    }
    
    server {
        location / {
            proxy_pass http://backend;
        }
    }
}

10。 网站打开后报错GET http://my_ip_address/_next/static/chunks/webpack-2d6c20b89449f43d.js net::ERR_ABORTED 403 (Forbidden)

nginx文件/var/log/nginx/error.log里面报错2023/12/31 17:32:49 [crit] 18018#18018: *17 stat() "/root/document/home/.next/static@public" failed (13: Permission denied), ...

这个报错是由于我把项目放在root项目下, centos的root下的项目,需要用用户root才能drwx,我把项目迁移到home下就好了

drwx------ 14 root root
  1. 错误日志在 /var/log/nginx/error.log

  2. alias和root的区别

location /admin {
    alias  /home/admin/dist;
}
#alias 会映射到/home/admin/dist

location /admin {
    root  /home/admin/dist;
}
#root 会映射到/home/admin/dist/admin

所以子项目用alias,根项目用root

  1. location的匹配, 可以先使用regex101.com/

/test/myapp/hello.php 和 /myapp/hello.php

location ~* /myapp/.+.php$ {
    #...
}

捕获组: 变量 $1 设置为 /myapp , $2 设置为 hello.php

location ~* (.*/myapp)/(.+.php)$ {
    #...
}

捕获组: 变量 $begin 设置为 /myapp , $end 设置为 hello.php 。

location ~* (?<begin>.*myapp)/(?<end>.+.php)$ {
    #...
}

我按着我的需求 www.domain.com/admin 或者 www.domain.com/admin/ 匹配第二条规则,www.domain.com/admin/post 或者 www.domain.com/admin/swipe… 让他重定向会第二条规则

# admin
location ~ /admin/.+ {
    try_files 301 /admin;
}

location /admin {
    alias  /home/admin/dist;
}

^/admin/(.+)$ 但是这条规则不是很讲道理

通过
/admin/swiper
/admin/post
/admin/
/admin

这样写会导致网页报错 将您重定向的次数过多,ERR_TOO_MANY_REDIRECTS, 虽然逻辑是对的,但是nginx往/admin下的路由,全都走第一条规则,造成无限循环,导致重定向太多。

location 可以看这篇文章stackoverflow.com/questions/5…

location optional_modifier location_match optional_modifier 有5种 = ^~ none ~ ~* 前3种都是字符串匹配, 后2种是正则,~区分大小写,~* 不区分

最后使用这个, 让它/admin/swiper 跳转到404页面,前端页面在判断路由在跳转回/admin, 解决这个问题, 所以nginx最好不写相同子路由的逻辑,正则没那么清晰

 location /admin {
    alias  /home/admin/dist;
    try_files $uri $uri/ /index.html /404.html;
}

根项目tsx代码, 404.html上

useEffect(() => {
    if (window.location.href.search('/admin/') > -1) {
      window.location.replace(`http://${location.host}/admin`)
    }
  }, [])
  1. http重定向到https
if ($scheme = "http") {
  return 301 https://$server_name$request_uri;
}