nginx配置,Nginx是一款轻量
nginx配置,Nginx是一款轻量
Nginx的安装和使用
本文是基于WIndows 10和CentOS 7.3系统环境,进行Nginx的安装和使用
- WIndows 10
- Nginx-1.16.1
- CentOS 7.3
一、nginx简介
(1) 什么是nginx
Nginx是一个高性能的http和反向代理web服务器。
Nginx是一款轻量级的web服务器·、反向代理服务器、及电子邮件(IMAP、POP3)代理服务器
特点:占有内存少、并发能力强,事实上nginx的并发能力确实在同类行的网页服务器中表现较好
Nginx专为性能优化而开发,性能是最重要的考量,实现上非常注重效率,能经受住高负载的考验,有报告表明能支持高达50000个并发连接数
Nginx支持热部署,它的启动特别容易,并且可以做到7*24不间断运行,即使运行数个月也不需要重启,还能在不间断服务的情况下对版本进行升级
(2) nginx的特点
- 正向代理
在客户端(浏览器)配置代理服务器,通过代理服务器访问互联网资源
- 反向代理
我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,再返回客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器的地址
- 负载均衡
单个服务器解决不了,我们增加服务器的数量,然后将请求分发到各个服务器,将原先的请求集中到单个服务器上的情况改为将请求分发到多个服务器上,将负载分发到不同的服务器,也就是我们所说的负载均衡。
- 动静分离
为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度,降低原来单个服务器的压力。
二、nginx安装和配置(Windows 10)
(1) 下载nginx安装包
- Nginx-1.16.1
(2) 解压安装包,并进入安装包的目录
解压目录不要有中文,也不要有空格
(3) 修改配置文件
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; server { listen 80; #监听80端口 server_name www.zs200a.com; #监听的域名 #charset koi8-r; #access_log logs/host.access.log main; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / { #转发或处理 root html; index index.html index.htm; } location /images/ { root D:\\code\itcast-haoke\haoke-upload; autoindex on; } error_page 500 502 503 504 /50x.html; #错误页面 location = /50x.html { root html; } } }
三、nginx的使用命令(Windows 10)
(1) 开启nginx
start nginx.exe
(2) 停止nginx
nginx.exe -s stop
(3) 重新加载nginx配置文件
nginx.exe -s reload
(4) 优雅地停止nginx
#会等当前所有任务结束后,停止nginx
nginx.exe -s quit
(5) 检查nginx是否启动
查看在logs目录下是否生成nginx.pid文件,nginx停止后会自动删除
四、nginx的安装和使用(CentOS 7.3)
(1) 安装准备软件
yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
(2) 下载nginx安装包
- Nginx-1.16.1
(3) 解压安装包
tar -zxvf nginx-1.16.1.tar.gz
(4) 编译并安装nginx
# 进入nginx-1.16.1目录 cd nginx-1.16.1 # 接下来安装,使用--prefix参数指定nginx安装的目录, ./configure --prefix=/opt/module/nginx-1.16.1 ./configure #默认安装在/usr/local/nginx make make install # 如果没有报错,顺利完成后,最好看一下nginx的安装目录 whereis nginx
五、Nginx设置成开机自启动命令
(1) 创建/etc/init.d/nginx文件
vi /etc/init.d/nginx
(2) 编辑/etc/init.d/nginx文件
其内容参考nginx官方文档,文件内容需要做如下的修改:
- 修改成nginx执行程序的路径
#chkconfig: - 85 15 #description: nginx is a World Wide Web server. It is used to serve nginx=”/usr/local/nginx/sbin/nginx”
- 修改成nginx.conf文件的路径
NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf”
(3) 修改/etc/init.d/nginx文件的权限
chmod a+x /etc/init.d/nginx
(4) 将nginx服务加入chkconfig管理列表
chkconfig --add /etc/init.d/nginx # 将nginx服务剔除chkconfig管理列表 chkconfig --del /etc/init.d/nginx
(5) 设置开机自启动
chkconfig nginx on # 关闭关机自启动 chkconfig nginx off
(6) 开启nginx
./nginx
(7) 停止nginx
./nginx -s stop
(8) 重新加载nginx配置文件
nginx -s reload
(9) 优雅地停止nginx
#会等当前所有任务结束后,停止nginx
nginx -s quit
六、Nginx出现403 forbidden
(1) 由于启动用户和nginx工作用户不一致所致
#查看nginx的启动用户 ps aux | grep "nginx: worker process" | awk'{print $1}' #如果一致,需要将nginx.config的user改为和启动用户一致 vi conf/nginx.conf
(2) 缺少index.html或者index.php文件
就是配置文件中index index.html index.htm这行中的指定的文件,如果在html目录下面没有index.htm,index.html的时候,直接访问文件,会报403 forbidden。
server { listen 80; #监听80端口 server_name www.zs200a.com; #监听的域名 location / { #转发或处理 root html; index index.html index.htm; } } } }
(3) 权限问题,如果nginx没有web目录的操作权限,也会出现403错误。
就是配置文件中index index.html index.htm这行中的指定的文件,如果在html目录下面没有index.htm,index.html的时候,直接访问文件,会报403 forbidden。
#解决办法:修改web目录的读写权限,或者是把nginx的启动用户改成目录的所属用户,重启Nginx即可解决 chmod -R 777 /data chmod -R 777 /data/www/
七、Nginx配置负载均衡
(1) 轮询(默认)
- nginx.conf
http { upstream myserver { server 172.23.16.100:8080; server 172.23.16.100:8081; } server { location / { Listen 80; server_name 172.23.16.100; proxy_pass http://myserver; proxy_connect_timeout 10; index index.html index.htm; } } }
(2) 权重
- nginx.conf
http { upstream myserver { server 172.23.16.100:8080 weight=1; server 172.23.16.100:8081 weight=2; } server { location / { Listen 80; server_name 172.23.16.100; proxy_pass http://myserver; proxy_connect_timeout 10; index index.html index.htm; } } }
(3) Hash 每一个请求按照访问IP的hash结果分配,每一个访客会固定访问一个服务器,可以解决session的问题
- nginx.conf
http { upstream myserver { ip_hash server 172.23.16.100:8080; server 172.23.16.100:8081; } server { location / { Listen 80; server_name 172.23.16.100; proxy_pass http://myserver; proxy_connect_timeout 10; index index.html index.htm; } } }
(4) Fair 根据后端服务器的响应时间来分配请求,响应时间短的优先分配
- nginx.conf
http { upstream myserver { server 172.23.16.100:8080; server 172.23.16.100:8081; fair; } server { location / { Listen 80; server_name 172.23.16.100; proxy_pass http://myserver; proxy_connect_timeout 10; index index.html index.htm; } } }
八、Nginx配置反向代理
(1) 二级域名代理
- nginx.conf
user root; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.zs200.com; location / { root /usr/local/nginx/zs200; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name zs200a.zs200.com; location / { root /usr/local/nginx/zs200a; index index.html; } } }
评论暂时关闭