稳定性高
系统资源消耗低
对http并发连接的处理能力高
对单台物理服务器可支持30000~50000个并发请求
基于账号密码
yum -y install pcre-devel zlib-devel gcc++ gcc #下载一些配置文件 [root@bogon ~]# useradd -M -s /sbin/nologin nginx #创建nginx用户-M是文件不被创建或复制 [root@bogon ~]# ls #上传软件包 anaconda-ks.cfg nginx-1.12.0.tar.gz test.sh [root@bogon ~]# tar zxf nginx-1.12.0.tar.gz -C /usr/src/ #解包 [root@bogon ~]# cd /usr/src/nginx-1.12.0/ #切换到这个目录 [root@bogon nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module #编译安装nginx Web服务器 [root@bogon nginx-1.12.0]# make &&make install #编译和安装Nginx [root@bogon nginx-1.12.0]# ls /usr/local/nginx/ conf html logs sbin [root@bogon nginx-1.12.0]# /usr/local/nginx/sbin/nginx #启动nginx [root@bogon nginx-1.12.0]# ss -nlpt |grep 80 #查询80端口有没有nginx [root@bogon nginx-1.12.0]# curl http://192.168.93.131 #访问nginx [root@bogon nginx-1.12.0]# /usr/local/nginx/sbin/nginx -s stop #停止nginx [root@bogon nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #设置软连接 [root@bogon nginx-1.12.0]# vi /etc/init.d/nginx #写一个脚本 #!/bin/bash # 必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10 # 90是启动优先级,10是停止优先级,优先级范围是0-100,数字越大,优先级越低 #chkconfig: 2345 10 90 #description:Nginx Service Control Script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0 chmod +x /etc/init.d/nginx #给脚本一个运行权限 chkconfig --add nginx #将nginx添加到服务列表中 systemctl status nginx #关闭nginx [root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf #nginx基础配置文件 在里面47行左右添加 location /status { stub_status on; } [root@bogon ~]# systemctl start nginx #启动nginx 然后去浏览器验证nginx是否成功 192.168.93.131/status yum install -y httpd-tools htpasswd -c /usr/local/nginx/passwd.db test 输入密码123456 再次输入密码123456 [root@bogon ~]# chmod 400 /usr/local/nginx/passwd.db [root@bogon ~]# chown nginx /usr/local/nginx/passwd.db [root@bogon ~]# ll -d /usr/local/nginx/passwd.db [root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf #打开nginx配置文件 #在47行左右的/status下面添加 auth_basic "secret"; # 添加认证 auth_basic_user_file /usr/local/nginx/passwd.db; # 指定认证的账户密码文件 [root@bogon ~]# systemctl restart nginx
[root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf #在status下面添加删除上次添加的两行 deny 192.168.93.131; allow all; [root@bogon ~]# systemctl restart nginx [root@bogon ~]# curl http://192.168.93.131/status
[root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf 在#gzip on;下面添加 server { listen 80; server_name www.kgc01.com; location /{ root html/kgc01; index index.html index.htm; } } server { listen 80; server_name www.kgc02.com; location /{ root html/kgc02; index index.html index.htm; } } [root@bogon ~]# mkdir /usr/local/nginx/html/kgc01 [root@bogon ~]# mkdir /usr/local/nginx/html/kgc02 [root@bogon ~]# echo 'This is kgc01' > /usr/local/nginx/html/kgc01/index.html [root@bogon ~]# echo 'This is kgc02' > /usr/local/nginx/html/kgc02/index.html [root@bogon ~]# systemctl restart nginx [root@bogon ~]# vi /etc/hosts 192.168.93.131 www.kgc01.com 192.168.93.131 www.kgc02.com [root@bogon ~]# curl www.kgc01.com This is kgc01 [root@bogon ~]# curl www.kgc02.com This is kgc02
[root@bogon ~]# vi /usr/local/nginx/conf/nginx.conf 在#gzip on;下面添加 server { listen 192.168.93.131:81; server_name 192.168.93.131:81; location /{ root html/kgc01; index index.html index.htm; } } server { listen 192.168.93.131:82; server_name 192.168.93.131:82; location /{ root html/kgc02; index index.html index.htm; } } nginx -t systemctl restart nginx
Nginx具有诸多优点,但在实际应用中仍需根据具体需求和环境进行配置和优化