本文共 7697 字,大约阅读时间需要 25 分钟。
目录
Nginx是一个轻量级/高性能的反向代理Web服务器,他实现非常高效的反向代理、负载平衡,他可以处理2-3万并发连接数,官方监测能支持5万并发,现在中国使用nginx网站用户有很多,例如:新浪、网易、 腾讯等。
systemctl stop firewalldsystemctl disable firewalldsetenforce 0iptables -F
tar xf nginx-1.15.9.tar.gz -C /opt
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module**********************省略make && make install
ln -s /usr/local/nginx/sbin/* /usr/local/bin
useradd -M -s /sbin/nologin nginx
nginx
#启动nginx后查看进程号ps -aux | grep nginxnetstat -natp | grep 80#未启动nginx时查看进程号cat /usr/local/nginx/logs/nginx.pid#杀死进程kill -l#退出nginxkill -s QUIT #或者killall -3 nginxkillall -l QUIT nginx################################################重载方式#############kill -3 kill -s HUP killall -l nginxkillall -s HUP
vim /etc/init.d/nginx#!/bin/bash# chkconfig: - 99 20 # chkcofig - “-” 表示不启用开机启动管理 (同时 若不加“#”, chkconfig add nginx 会加载不到配置)# description: Nginx Service Control ScriptCOM="/usr/local/nginx/sbin/nginx" #命令程序文件位置(nginx)PID="/usr/local/nginx/logs/nginx.pid" #pid文件case "$1" instart) $COM ;;stop) kill -s QUIT $(cat $PID) ;;restart) $0 stop $0 start ;;reload) kill -s HUP $(cat $PID) ;;*) echo "Usage: $0 {start|stop|restart|reload}" exit 1esacexit 0
chmod +x /etc/init.d/nginxchkconfig --add nginx #添加为系统服务systemctl stop nginxsystemctl start nginx
vim /usr/lib/systemd/system/nginx.service[Unit] Description=nginx #描述After=network.target #描述服务类别[Service]Type=forking #后台运行类型PIDFile =/usr/local/nginx/logs/nginx.pid #PID文件位置ExecStart=/usr/local/nginx/sbin/nginx #启动服务ExecrReload=/bin/kill -s HUP $MAINPID #根据PID重载配置ExecrStop=/bin/kill -s QUIT $MAINPID #根据PID终止进程PrivateTmp=true[Install]WantedBy=multi-user.target #启动级别chmod 754 /lib/systemd/system/nginx.service #设置754权限是一种安全优化systemctl start nginx.servicesystemctl enable nginx.service
worker_processes 1; # worker进程的数量events { # 事件区块开始 worker_connections 1024; # 每个worker进程支持的最大连接数} # 事件区块结束http { # HTTP区块开始 include mime.types; # Nginx支持的媒体类型库文件 default_type application/octet-stream; # 默认的媒体类型 sendfile on; # 开启高效传输模式 keepalive_timeout 65; # 连接超时 server { # 第一个Server区块开始,表示一个独立的虚拟主机站点 listen 80; # 提供服务的端口,默认80 server_name localhost; # 提供服务的域名主机名 location / { # 第一个location区块开始 root html; # 站点的根目录,相当于Nginx的安装目录 index index.html index.htm; # 默认的首页文件,多个用空格分开 } # 第一个location区块结果 error_page 500502503504 /50x.html; # 出现对应的http状态码时,使用50x.html回应客户 location = /50x.html { # location区块开始,访问50x.html root html; # 指定对应的站点目录为html } }
注:此处worker_connections 1024; 每个worker进程支持的最大连接数 和系统的文件打开数有一定关系,所以在优化的时候,需要通过ulimit -a查看文件打开数,如图
可以通过ulimit -n 65535 临时修改为最大文件打开值,那么如何永久修改呢,请看小节:3.5.永久修改文件打开最大值
#要想ulimits 的数值永久生效,必须修改配置文件/etc/security/limits.conf #在该配置文件中添加* soft nofile 65535 * hard nofile 65535 echo "* soft nofile 65535" >> /etc/security/limits.confecho "* hard nofile 65535" >> /etc/security/limits.conf* 表示所用的用户
通过/usr/local/nginx/sbin/nginx -V 查看HTTP_STUB_STATUS的模块是否存在,如果不存在,请重新./configure
#修改nginx.conf主配置文件#在server中,新增一个location,添加方式如下:location /status { stub_status on; #开启统计功能 access_log off; #关闭日志记录}
访问方式:http://192.168.1.20/status
其中三个数字表示:已经处理的连接数 、成功的TCP握手次数、已经处理的请求数
yum install -y httpd-toolshtpasswd -c /usr/local/nginx/passwd. db zhangsan#添加nginx管理、给与400权限chown nginx /usr/ local/nginx/passwd.dbchmod 400 /usr/local/nginx/passwd.db#修改主配置文件相对应目录,添加认证配置项vim /usr/local/nginx/conf/nginx.conflocation /status { auth_basic "secret"; auth_basic_user_file /user/local/nginx/passwd.db ****************** 省略}
#修改主配置文件相对应目录,添加认证配置项vim /usr/local/nginx/conf/nginx.conflocation / { root html; index index.html index.htm; deny 192.168.1.20; #限制192.168.1.20 访问 allow all; #放通其他来源的访问}
echo "192.168.1.17 www.accp.com www.betnet" >> /etc/hosts
mkdir -p /var/www/html/accpmkdir -p /var/www/html/benetecho "www.accp.com
" >> /var/www/html/accp/index.htmlecho "www.benet.com
" >> /var/www/html/benet/index.html
server { # 第一个Server区块开始,表示一个独立的虚拟主机站点 listen 192.168.1.17:80; # 提供服务的端口,默认80 server_name www.accp.com; # 提供服务的域名主机名 charset utf-8; access_log logs/accp.access.log; location / { # 第一个location区块开始 root /var/www/html/accp; # 站点的根目录,相当于Nginx的安装目录 index index.html index.htm; # 默认的首页文件,多个用空格分开 } # 第一个location区块结果 server { listen 192.168.1.17:80; server_name www.benet.com; charset utf-8; access_log logs/accp.access.log; location / { root /var/www/html/benet; index index.html index.htm; }
server { listen 192.168.1.17:80; server_name www.accp.com; charset utf-8; access_log logs/accp.access.log; location / { root /var/www/html/accp; index index.html index.htm; } server { listen 192.168.1.17:8080; server_name www.benet.com; charset utf-8; access_log logs/accp.access.log; location / { root /var/www/html/benet; index index.html index.htm; }
vim /etc/hosts192.168.1.17 www.accp.com192.168.1.100 www.benet.com
ifconfig en33:0 192.168.1.100 netmask 255.255.255.0
server { listen 192.168.1.17:80; server_name www.accp.com; charset utf-8; access_log logs/accp.access.log; location / { root /var/www/html/accp; index index.html index.htm; } server { listen 192.168.1.100:80; server_name www.benet.com; charset utf-8; access_log logs/accp.access.log; location / { root /var/www/html/benet; index index.html index.htm; }
在整个部署,配置过程,大家可以清晰看出,Nginx作为静态访问强大的服务,其配置也是相对比较复杂的,但好在清晰,而Apache的配置相对来说就较为简单,使用与不使用都有总的主配置文件进行控制。当然Nginx在配置的灵活性上要高的多,毕竟可以在server中对各个页面进行做访问控制和功能模块添加。
转载地址:http://vwucn.baihongyu.com/