centos7中使用yum安装NGINX及简单配置

工作中的web服务的反向代理、负载均衡、ip白名单、静态资源管理可以使用NGINX,其安装步骤可以参考如下。服务器为centos7,用户使用root权限。
1. 安装第三方yum源

yum install -y epel-release

2. 安装NGINX

yum install -y nginx

3. 关闭防火墙-可选

systemctl stop firewalld

4. 防止防火墙开机自启-可选

systemctl disable firewalld

5. 配置ip白名单
可在项目主目录下配置
vim /testdir/testproject/white_ip_list.conf

white_ip_list.conf具体内容如下:
127.0.0.1 1;
0.0.0.0 1;


5. 基本的NGINX配置

最好在NGINX安装目录conf.d目录下创建testproject.conf文件。在NGINX主目录nginx.conf文件下include该文件。
具体testproject.conf创建方式如下。

# 配置后台任务
upstream postendapi {
    server  0.0.0.0:8379;
}
# 配置ip白名单

geo $is_white_ip {
    default 0;
    include /testdir/testproject/white_ip_list.conf;
}
server {
        # 配置最大传输字节
        client_max_body_size 1024M;
        location / {
            # 配置项目主目录
            root /testdir/testproject;
            try_files $uri $uri/ @router;
            index index.html;
            if ( $is_white_ip = 0 ) {
                return 403;
            }
        }
        location @router {
            # vue项目配置index文件,其他框架仅供参考
            rewrite ^.*$ /index.html last;
        }
        # 配置分路径1
        location ^~ /testproject1/ {
            proxy_set_header x-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_pass http://postendapi;
        }
        # 配置分路径2
        location ^~ /testproject2/ {
            proxy_set_header x-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_pass http://postendapi;
        }
}

 


6. 启动NGINX

systemctl start nginx

7. 查看NGINX状态

systemctl status nginx

8. NGINX更改配置后重载

nginx -s reload
 
9. NGINX停止

nginx -s stop
 
posted @ 2024-06-17 10:39  stone9693  阅读(171)  评论(0)    收藏  举报
GitHub账户:https://github.com/stone9693