0%

CentOS安装Nginx并配置转发

转发效果

请求 http://www.yourdomain.com/
转发到 http://www.yourdomain.com:8080/

安装Nginx

一、配置 EPEL源

1
sudo yum install -y epel-release
2
sudo yum -y update

二、安装Nginx

1
sudo yum install -y nginx

安装成功后,默认的网站目录为: /usr/share/nginx/html

默认的配置文件为:/etc/nginx/nginx.conf

自定义配置文件目录为: /etc/nginx/conf.d/

三、开启端口80和443

如果你的服务器打开了防火墙,你需要运行下面的命令,打开80和443端口。

1
sudo firewall-cmd --permanent --zone=public --add-service=http
2
sudo firewall-cmd --permanent --zone=public --add-service=https
3
sudo firewall-cmd --reload

找到Nginx配置文件

1
[root@localhost ~]# nginx -t
2
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
3
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

修改nginx配置文件

1
...
2
server{
3
       ...
4
       #...默认转发至8080端口 Satrt 
5
        location / {
6
                 proxy_pass http://localhost:8080;
7
        }
8
       #...默认转发至8080端口 End
9
        location /nginx_status
10
        {
11
            stub_status on;
12
            access_log   off;
13
        }
14
        ...
15
     }
16
...
17
}

验证配置文件是否正确

1
nginx -t

使配置文件生效

1
nginx -s reload

Nginx操作命令

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