Nginx
在Linux上部署nginx
下载nginx压缩包
下载网站 | nginx: download
在Linux操作压缩包
nginx需要相关编译环境
#解压
tar -zxvf nginx-1.21.6.tar.gz
# 下载c++环境
yum install -y gcc
# 下载其他环境
yum -y install pcre-devel
yum -y install openssl openssl-devel
# 将nginx配置到/usr/local路径下
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
# 编译
make
# 安装
make install
# 切换到/usr/local路径下
cd /usr/local
开启ssl服务需要 –with-http_ssl_module
nginx相关使用
启动nginx
# 启动文件在sbin目录下
cd /usr/local/nginx/sbin
# 启动
./nginx
使用浏览器进行访问
nginx命令
# 进入nginx的sbin目录下
# 启动nginx的命令为
./nginx
# 停止nginx的命令为
./nginx -s stop
# 重新加载nginx配置
./nginx -s reload
# 优雅关闭
./nginx -s quit
将nginx的命令配置到systemctl中
# 在 /usr/lib/systemd/system/目录下面新建一个nginx.service文件
vim /usr/lib/systemd/system/nginx.service
# 赋予可执行的权限
chmod +x /usr/lib/systemd/system/nginx.service
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
reload systemctl
#在启动服务之前,需要先重载systemctl命令
systemctl daemon-reload
## 加载后的命令
# 启动nginx
systemctl start nginx
# 查看nginx状态
systemctl status nginx
# 停止nginx服务
systemctl stop nginx
# 重新加载配置文件
systemctl reload nginx
nginx.conf配置
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
#监听在80端口
listen 80;
#域名或者ip地址
server_name localhost;
location / {
#访问路径 html为文件夹
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
测试
# 在根目录创建www文件夹
mkdir www
# 进入www文件夹
cd www
# 编写主页面
vim index.html
index.html内容
hello , this is www site.
配置nginx.conf文件
systemctl reload nginx 更新nginx配置
注意:配置的root是/www,是从根目录开始寻找,原配置是html,从nginx目录开始寻找
反向代理
proxy_pass
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
#监听在80端口
listen 80;
#域名或者ip地址
server_name localhost;
location / {
#当存在proxy_pass时,下面内容不再有用
proxy_pass http://www.baidu.com;
#root html;
#index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
浏览器再次访问
将其他端口应用映射到80端口
server{
listen 80;
server_name xxxx_xxx;
location / {
proxy_redirect off;
proxy_pass http://ip:端口;
}
}
注:xxxx_xxx可以是公网ip也可以是域名
下面为例子
# 将text.com 8090端口的halo服务反向代理到80端口
server{
listen 80;
server_name test.com;
location / {
proxy_redirect off;
proxy_pass http://test.com:8090;
}
}
负载均衡
upstream
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream httpds{
server 192.168.1.101 weight=5 down;
server 192.168.1.102 weight=3 backup;
}
server {
#监听在80端口
listen 80;
#域名或者ip地址
server_name localhost;
location / {
#当存在proxy_pass时,下面内容不再有用
proxy_pass http://httpds;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
- weight : 权重 数值越高,载荷越高,请求数越大
- down : 该服务器停止使用,请求不再经过该服务器
- backup : 其他服务器运行正常时,不经过该服务器,其他服务器全部停止时,该服务器提供服务
当我们想只通过nginx访问服务器,不能直接访问服务器时,配置防火墙规则
例:nginx(192.168.1.100:80)代理Tomcat(192.168.1.101:8080)服务时,但是不想直接通过浏览器访问Tomcat,可以在Tomcat服务器配置防火墙规则
重启服务器
systemctl restart firewalld
指定ip和端口访问
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="8080" accept"
移除规则
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="8080" accept"
重载规则
firewall-cmd --reload
查看已配置的规则
firewall-cmd --list-all