nginx


下载

  1. 下载nginx
  2. 下载依赖
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel pcre pcre-devel
  1. 将下载好的nginx上传至服务器
  2. 解压,编译,安装
# 解压
tar zxvf nginx-1.21.6.tar.gz
# 进入nginx安装包
cd nginx-1.21.6
# 源码安装,自定义安装位置
./configure --prefix=/usr/local/nginx
# 编译安装
make
make install
  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
  1. 重新加载系统服务
systemctl daemon-reload
  1. 基本命令
# 开机自启
systemctl enable nginx

# 启动
systemctl start nginx

# 重新启动
systemctl restart nginx

# 关闭
systemctl stop nginx

# 查看运行状态
systemctl status nginx

配置

根据域名转发

添加多个server,server_name用 域名做区分


  server {
        listen       80;
        server_name  a.clcoding.cn;

        location / {
	   proxy_pass http://www.clcoding.cn;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
	
	  server {
        listen       80;
        server_name  b.clcoding.cn;

        location / {
	   proxy_pass http://www.baidu.com;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

多机器负载均衡

在与server同级定义upstream

 upstream myservices {
     server 127.0.0.1:8080;
     server 127.0.0.1:8081;
 }

使用proxy_pass,选择定义的upstream

server {
        listen       80;
        server_name  b.clcoding.cn;

        location / {
	   proxy_pass http://myservices;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }