正向代理:代理的是客户端,例如vpn
反向代理:代理的是服务器,例如百度域名
负载均衡:轮询【每台服务器一人一下】:可设置权重weight,根据权重比例访问,
如果设置 down 指定服务器下线, backup备用服务器
第一种:【无法指定安装目录】
Nginx同样需要配置额外的yum仓库,才可以使用yum安装
安装Nginx的操作需要root身份
yum install -y yum-utils // 软件包安装
vim /etc/yum.repos.d/nginx.repo // yum程序使用的仓库配置文件
centos填入如下内容并保存退出
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
安装nginx
yum install -y nginx
nginx的配置文件在/etc/nginx/
项目文件存放在/usr/share/nginx/
日志文件存放在/var/log/nginx/
第二种【可指定安装目录】
本地压缩包上传方式
cd /root
XFTP上传压缩包
tar zxvf 压缩包名 // tar压缩包解压 【请根据压缩包类型使用不同命令进行解压】
cd /nginx-1.24.0
ls // 查看是否含有configure 配置文件
./configure --prefix=/root/nginx // 检测安装平台目标特征,生成Makefile 【--prefix指定安装目录】一般设置在 /usr/local/nginx,不会报403错误
运行报错,安装对应依赖在执行 ./configure --prefix=/root/local/nginx 查看是否还有未安装依赖
安装 yum install -y gcc
checking for OS + Linux 3.10.0-693.el7.x86_64 x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found
安装 yum install -y pcre pcre-devel
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option.
# 安装 yum install -y zlib zlib-devel
./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option.
make // 自动编译管理器【根据Makefile进行编译规范】
make install // 安装
cd /root/nginx/sbin
./nginx // 启动nginx服务[根据ip进行访问] 启动失败。设置防火墙
ip访问显示403错误 【设置文件权限【未测试】】
将nginx.config的user改为和启动用户一致
ps aux | grep nginx // 查看启动用户 cd /root/nginx/conf vim nginx.conf # 修改第一行 user root # 保存退出 ./nginx // 重新运行 ./nginx -s stop //停止运行 ./nginx -s reload // 保持运行,重新加载配置 ./nginx -s quit // 在退出前完成已经接受的连接请求
使用服务脚本开启nginx
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=/root/nginx/logs/nginx.pid ExecStartPre=/root/nginx/sbin/nginx -t -c /root/nginx/conf/nginx.conf ExecStart=/root/nginx/sbin/nginx -c /root/nginx/conf/nginx.conf ExecReload=/root/nginx/sbin/nginx -s reload ExecStop=/root/nginx/sbin/nginx -s stop ExecQuit=/root/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
systemctl daemon-reload // 重新加载系统服务
ps -ef | grep nginx // 查看进程 【开启的话请先停止运行】
cd /root/nginx/sbin
./nginx -s stop
systemctl start nginx // 使用系统服务运行
常用命令
nginx自动注册了systemctl系统服务
systemctl start nginx // 启动
systemctl stop nginx // 停止
systemctl status nginx // 运行状态
systemctl enable nginx // 开机自启
systemctl disable nginx // 关闭开机自启
防火墙配置
nginx默认绑定80端口,需要关闭防火墙或放行80端口
systemctl stop firewalld // 关闭防火墙
systemctl disable firewalld // 关闭开机自启
firewall-cmd --add-port=80/tcp --permanent // 放行tcp规则下的80端口,永久生效
firewall-cmd --reload // 重新加载防火墙规则
启动后浏览器输入Linux服务器的IP地址或主机名即可访问
80端口是访问网站的默认端口,所以后面无需跟随端口号
http://192.168.xx.xxx 或 http://centos
配置文件nginx.conf
多个站点配置
user nginx;
# 工作进程数
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
# 请求头类型
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
# 引用其他配置文件
# include /etc/nginx/conf.d/*.conf;
#一个nginx可以启用多个server(虚拟服务器)
server {
listen 80;#监听端口80
server_name localhost; #接收的域名
location / {
root html; #根目录指向html目录
index index.html index.htm; #域名/index 指向 index.html index.htm文件
}
error_page 500 502 503 504 /50x.html;
# 服务器错误码为500 502 503 504,转到"域名/50x.html"
location = /50x.html {# 指定到html文件夹下找/50x.htm
root html;
}
}
server {
listen 88;
server_name *.test.com; # 域名可用完整、通配符(结束)、正则匹配
location / {
root /www/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
hosts文件
添加ip 域名访问
192.168.xxx.xxx test.com
反向代理 、负载均衡
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 负载均衡
upstream httpds{
server 192.168.111.132 weight=8 down;
server 192.168.111.133 weight=2;
server 192.168.111.135 weight=1 backup;
}
server {
listen 80;
server_name localhost;
location / {
# url地址重写
# rewrite ^/([0-9]+).html$ /index.jsp?pageNum=$1 break;
proxy_pass http://httpds;
# 反向代理
# proxy_pass http://192.168.111.133;
# root html;
# index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
特殊:ip_hash 同一台服务器,保持会话
least_conn 最少连接访问
url_hash 根据用户访问的url定向转发请求
fair 根据后端服务器响应的时间转发请求
学习地址