CentOS 7 安装Nginx并进行简单的代理配置

VMware版本:16pro

Linux系统:CentOS 7.4 mini版

连接工具:XShell 7

首先确保Linux系统能连上网,可以看我的上一篇文章。

1. 下载安装Nginx

1.1 下载Nginx压缩包,我使用的是1.21.6版本,稍后给出下载地址

1.2 通过Xftp 7将压缩包拖到Linux系统的某一目录下

1.3 解压缩

[root@localhost ~]# tar -xzvf nginx-1.21.6.tar.gz

1.4. 安装必要组件

编译Nginx之前需要安装gcc,perl库,zlib库,没安装的话可以通过以下命令安装:

yum install -y gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel

1.5 安装成功过后进入解压后的文件夹,输入以下命令安装:

./configure --prefix=/usr/local/nginx
make
make install

1.6 进入安装好的目录

cd /usr/local/nginx/sbin

nginx启停常用命令:

./nginx 启动Nginx
./nginx -s stop 停止Nginx
./nginx -s quit 在退出前完成已经接受的连接请求
./nginx -s reload 重新加载

2. 关闭防火墙

2.1 查看防火墙状态

[root@localhost nginx-1.21.6]# systemctl status firewalld

关闭后的状态是这样的:

2.2 关闭防火墙命令

systemctl stop firewalld.service

 2.3 禁止防火墙开机启动

systemctl disable firewalld.service

2.4  放行80端口

firewall-cmd --zone=public --add-port=80/tcp --permanent

2.5 重启防火墙

firewall-cmd --reload

3. 设置开机启动

3.1 创建服务脚本

vi /usr/lib/systemd/system/nginx.service

粘贴以下内容:

[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

顺带说一下 vi 或 vim 使用命令:

按i键:进入编辑状态
按esc键:进入命令状态
命令状态下,输入:q后回车代表直接退出但不保存
输入:q!后回车代表强制退出不保存
输入:wq!后回车代表保存后强制退出

3.2 重新加载并启动系统服务

systemctl daemon-reload
systemctl start nginx.service

3.3 设置开机启动

systemctl enable nginx.service

4. 配置Nginx代理

打开nginx安装目录的conf文件夹

cd /usr/local/nginx/conf

编辑 nginx.conf 文件

vi nginx.conf
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8001;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
root   html;
            index  index.html index.htm;
        }

        location ~/test/ {
            proxy_pass http://localhost:8080/;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

其实就是在http大括号下的server大括号内加一段

        location ~/test/ {
            proxy_pass http://localhost:8080/;
        }

代表访问路径带有test的话就走http://localhost:8080/;这个地址。

注意监听端口和ip也要改

listen       8001;
server_name  localhost;

假如把springboot项目运行在8080端口中,linux系统的ip假设为192.168.100.100,那么只需要在物理机浏览器访问192.168.100.100:8001/test即可访问到运行在虚拟机的8080端口的项目了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值