下载安装包
wget https://nginx.org/download/nginx-1.20.1.tar.gz
安装依赖包
yum install pcre pcre-devel -y
yum install openssl openssl-devel -y
解压安装包并编译
tar -xf nginx-1.20.1.tar.gz
cd nginx-1.20.1
./configure --with-http_ssl_module --with-http_v2_module --with-stream
--with-http_ssl_module
--with-http_v2_module
--with-stream
make install
加入环境变量
vi /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin
source /etc/profile
测试
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/372a303469ab492e9ea4e0ef278dcbae.png)
system系统管理
vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
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
[Install]
WantedBy=multi-user.target
重启服务
systemctl daemon-reload
systemctl start nginx
注释:重启服务报错,可能是端口占有,netstat -tnulp | grep 80 看一下进程 kill掉重启
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/321f816f057f4006b28803c44a3478ad.png)
配置多个 .conf文件
mkdir conf.d
cd /usr/local/nginx/conf
vi nginx.conf
http {
server {
}
include /usr/local/nginx/conf/conf.d/*.conf;
}
cd conf.d
vi my.conf
server {
listen 8888;
server_name localhost;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8999;
}
}
nginx -t
nginx -s reload
nginx.conf简化
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include /usr/local/nginx/conf/conf.d/*.conf;
}