CentOS 安装和使用最新版 Nginx

安装最新版 Nginx

配置 EPEL 源,否则安装的是旧的或找不到Nginx源

1
sudo yum install -y epel-release

安装Nginx

1
sudo yum install -y nginx

启动 Nginx

1
systemctl start nginx

访问: http://localhost, Nginx 默认端口是80,可以省略 https://raw.gitmirror.com/telzhou618/images/main/img03/img.png

Nginx 常用命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12

# 检查 nginx 配置是否正确
nginx -t

# 停止服务
nginx -s stop

# 退出服务
nginx -s quit

# 重启服务
nginx -s reload

用 systemctl 操作

1.启动 Nginx

1
systemctl start nginx

2.停止 Nginx

1
systemctl stop nginx

3.重启 Nginx

1
systemctl restart nginx

4.查看 Nginx 状态

1
systemctl status nginx

5.启用开机启动 Nginx

1
systemctl enable nginx

6.禁用开机启动 Nginx

1
systemctl disable nginx

配置域名转发

cd 到 /etc/nginx/conf.d 目录,创建配置文件,创建后缀为conf的文件,这里以gitlab服务为例,假如 gitlab 的IP端口为 http://192.168.1.4:10008

1
2
cd /etc/nginx/conf.d
vi www.gitlab.net.conf

键入以下配置

1
2
3
4
5
6
7
8
server {
    listen 80;
    server_name  www.gitlab.net;

    location / {
        proxy_pass http://192.168.1.4:10008;
    }
}

保存,重启 Nginx

1
2
3
4
# 测试配置
nginx -t  
# 无报错重启
nginx -s reload 

修改本机 hosts 测试

1
192.168.1.4 www.gitlab.net

访问 www.gitlab.net 测试,ok!

https://raw.gitmirror.com/telzhou618/images/main/img03/20240423004633.png

0%