nginx官方下载地址: http://nginx.org/en/download.html
参考博客: Centos设置nginx开机自启动_Willis_m的博客-CSDN博客_centos nginx自启动
启动服务报错解决办法:https://blog.csdn.net/li1325169021/article/details/118462921
备注:报错原因,自己先手动启动Nginx,已启用端口被占用后启动服务就会报错 :Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
一、安装nginx前置软件
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
如果没有网络会报错
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
12: Timeout on http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock: (28, 'Resolving timed out after 30553 milliseconds')
解决办法
阿里云开源镜像站:
http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/
centos7 离线安装gcc pcre-devel openssl-devel zlib-devel:
https://blog.csdn.net/wxg198286/article/details/122194881
二、下载nginx安装包,解压并安装
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
# 设置安装目录为/usr/local/nginx
./configure --prefix=/usr/local/nginx
make
make install
三、启动、重启nginx
/usr/local/nginx/sbin/nginx # 启动
pkill -9 nginx # 关闭
/usr/local/nginx/sbin/nginx -s reload # 重启
# 检查配置是否正常命令
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -t
四、nginx.conf配置include,多模块引用
基本配置:https://blog.csdn.net/zhanglinlang/article/details/88374133
多模块引用:https://blog.csdn.net/yanyf2016/article/details/116006885
cat /usr/local/nginx/conf/nginx.conf
user root;
worker_processes 1;
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 /data/logs/nginx/access.log main;
sendfile on; # 开启高效文件传送模式
keepalive_timeout 65; # 连接超时时间: 单位 秒
#gzip on; # 开启gzip 压缩输出
include /usr/local/nginx/conf/conf-lanren312/*.conf;
# server {
# listen 80;
# server_name 192.168.0.119;
# location / {
# root html;
# index index.html index.htm;
# }
}
#stream模块和http模块是并列级别
stream {
server {
listen port;
proxy_connect_timeout 5s;
proxy_timeout 300s;
proxy_pass ip:port;
}
}
暂时补充两个,以后可能会用
stream {
upstream back{
server ip:port;
}
server {
listen port udp;
proxy_connect_timeout 5s;
proxy_timeout 300s;
proxy_pass back;
}
}
stream {
server {
listen 123456; # 配置Nginx监听的端口
proxy_pass backend; # 定义后端服务器群组名为backend
}
upstream backend {
server 127.0.0.1:666; # 这里配置转发的后端服务器地址和端口
}
}
Nginx正在监听123456端口,并将流量转发到后端服务器127.0.0.1的666端口
cat /usr/local/nginx/conf/conf-lanren312/lanren312.conf
upstream lanren312 { # 和proxy_pass内的名称一致 ,负载均衡用,不用可不写
server 192.168.0.119:9071 weight=1; #weight:权重,默认是1,数值越大提供服务的次数就越多
}
server {
listen 80;
server_name 192.168.0.119; # 我本地虚拟机ip
access_log /data/logs/nginx/lanren312/access.log;
error_log /data/logs/nginx/lanren312/error.log;
root /data/lanren312/; # 118 也这么配置
index index.html index.htm;
location / {
root /data/lanren312/;
index index.html index.htm;
}
location /java/ {
# proxy_pass http://192.168.0.119:9071/; # 直接写法
proxy_pass http://lanren312; # 和upstream一致 ,负载均衡用,不用可不写
#Proxy Settings
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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /118/ {
proxy_pass http://192.168.0.118:80/;
# yml配置
# filepath: http://192.168.0.119/118/file ,就能访问到118上/data/lanren312/file里面的内容
}
}
五、设置开机自启动
vim /lib/systemd/system/nginx.service
######## 写入内容
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl enable nginx # 加入开机自启动
systemctl disable nginx # 取消开机自启动
systemctl start nginx.service # 启动nginx服务
systemctl stop nginx.service # 停止服务
systemctl restart nginx.service # 重新启动服务
systemctl list-units --type=service # 查看所有已启动的服务
systemctl status nginx.service # 查看服务当前状态
systemctl enable nginx.service # 设置开机自启动
systemctl disable nginx.service # 停止开机自启动
shutdown -r now # 重启服务器,nginx自启动成功
六、查看版本号
/usr/local/nginx/sbin/nginx -v
七、Windows下启动、重启、停止nginx
在Windows下操作nginx,需要打开cmd 进入到nginx的安装目录下
1.启动nginx:
start nginx 或 nginx.exe
2.停止nginx(stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息)
nginx.exe -s stop 或 nginx.exe -s quit
3.检查 重启:
nginx -t 修改nginx配置后执行检查配置是否正确
nginx -s reload 重启
八、升级nginx到 1.23.1
centos7 nginx1.18.0升级到1.23.1_lanren312的博客-CSDN博客
九、防火墙
centos7会默认开启防火墙,要在防火墙中打开对应的端口
firewall-cmd --state ## 查看防火墙状态
firewall-cmd --list-ports ## 查看已经开放的端口
firewall-cmd --zone=public --add-port=80/tcp --permanent ## 开启80端口
firewall-cmd --reload ## 重启防火墙 (重新开启端口后一定要重启防火墙,再查看开放的端口)