Nginx网站服务

  • Post author:
  • Post category:其他




Nginx概述



一款高性能、轻量级Web服务软件

  • 稳定性高
  • 系统资源消耗低
  • 对HTTP并发连接的处理能力高
  • 单台物理服务器可支持30000 ~ 50000个并发请求
  • 占用内存少,并发能力强



Nginx相对于Apache的优点

  • 轻量级,同样起web 服务,比apache 占用更少的内存及资源
  • 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx 能保持低资源低消耗高性能
  • 高度模块化的设计,编写模块相对简单
  • 社区活跃,各种高性能模块出品迅速啊



Apache相对于Nginx的优点

  • rewrite ,比nginx 的rewrite 强大
  • 模块超多,基本想到的都可以找到
  • 少bug ,nginx 的bug 相对较多
  • 超稳定



编译安装Nginx 服务



1、关闭防火墙并将压缩包解压到opt目录下



2、安装依赖包

yum -y install pcre-devel zlib-devel gcc gcc-c++ make



3、创建运行用户、组


(Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限)

useradd -M -s /sbin/nologin/ nginx	 #-M 代表不创建家目录



4、编译安装 Nginx

tar zxvf nginx-1.12.2.tar.gz -C /opt/

cd /opt/nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

#每行代表的意思
./configure \
--prefix=/usr/local/nginx \      #指定nginx的安装路径
--user=nginx \        #指定用户名
--group=nginx \      #指定组名
--with-http_stub_status_module   #启用 http_stub_ status_ module 模块以支持状态统计

make -j2 && make install    启用两核处理器

ln -s /usr/local/nginx/sbin/* /usr/local/sbin/     #创建软链接便于系统管理,让系统识别nginx的操作命令



5、检查、启用、重启、停止 nginx服务

nginx -t     #检查配置文件是否配置正确

nginx     #启动
#####这里启动服务如果启动失败可以查看apache的服务是否开启,如果已经开启可以通过service httpd stop

关闭服务
----停止nginx 服务------
cat /usr/local/nginx/logs/nginx.pid    #首先查看nginx的PID号
kill -3  <PID号>
kill -s QUIT <PID号>
killall -3 nginx
killall -s QUIT nginx


----重载------
kill -1 <PID号>
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP <PID号>

----日志分隔,重新打开日志文件-------
Kill -USR1 <PID号>

-----平滑升级------
kill -USR2 <PID号>


查看PID号的几种方式

  • 方法一:

    [root@www ~]# netstat -natp | grep :80

    tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 49796/nginx: master

  • 方法一:

    [root@www ~]# cat /usr/local/nginx/logs//nginx.pid

    49796

  • 方法二:

    [root@www ~]# ps aux | grep nginx

    root 49796 0.0 0.0 20496 604 ? Ss 15:01 0:00 nginx: master process nginx

    nginx 49797 0.0 0.0 23024 1388 ? S 15:01 0:00 nginx: worker process

    root 49890 0.0 0.0 112676 980 pts/0 S+ 15:10 0:00 grep –color=auto nginx

  • 方法三:

    [root@www ~]# ss -lnpt | grep :80

    LISTEN 0 128 *:80

    :

    users:((“nginx”,pid=49797,fd=6),(“nginx”,pid=49796,fd=6))

  • 方法四:

    [root@www ~]# lsof -i :80

    COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

    nginx 49796 root 6u IPv4 131812 0t0 TCP *:http (LISTEN)

    nginx 49797 nginx 6u IPv4 131812 0t0 TCP *:http (LISTEN)



6、添加 Nginx 系统服务


方法一

vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
   $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 1
esac
exit 0
chmod +x /etc/init.d/nginx
chkconfig --add nginx         #添加为系统服务
systemctl stop nginx
systemctl start nginx


方法二

vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
---------------------------------------------------------
#每行意义
[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 daemon-reload        ##这里需要重载一下,不然可能会启动失败
systemctl start nginx.service
systemctl enable nginx.service

在这里插入图片描述



认识Nginx服务的主配置文件 nginx.conf

cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf



1、全局配置

#user nobody;     #运行用户,若编译时未指定则默认为 nobody
worker_ processes 1;   #工作运行数量,可配置成服务器内核数*2,如果网站访问量不大,一般设为1就够用了
#error_log logs/error.log;  #错误日志文件的位置
#pid logs/nginx.pid;   #PID文件的位置



2、I/O事件配置

events {
  use epoll;              #使用 epoll 模型,2.6及以上版本的系统内核,建议使用 epoll 模型以提高性能
  worker_connections 4096;    #每个进程处理 4096 个连接
}

#如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数。
#在Linux 平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
#可使用ulimit -a 命令查看系统允许当前用户进程打开的文件数限制。



3、HTTP 配置

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;
    ##此项允许或禁止使用socket的TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
    #tcp_nopush     on;
    ##连接保持超时时间,单位是秒
    #keepalive_timeout  0;
    keepalive_timeout  65;              #连接保持超时
    ##gzip模块设置,设置是否开启gzip压缩输出
    #gzip  on;
##web 服务的监听配置
    server { 
        listen       80;               #监听地址及端口
        server_name  www.gcc.com;      #站点域名,可以有多个,用空格隔开
        #charset koi8-r;        #网页的默认字符集  
        location / {           #根目录配置
            root   html;       #网站根目录的位置/usr/local/nginx/html
            index  index.html index.htm;   #设置首页文件名
        }
        ##内部错误的反馈页面
        error_page  500 502 503 504 /50x.html;
        ##错误页面配置
        location = /50x.html {
             root   html;
        }
      }
  }

在这里插入图片描述

在这里插入图片描述



日志格式设定:

$remote_addr  $http_x_forwarded for用以记录客户端的ip地址;
sremote_user:用来记录客户端用户名称;
stime_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent:记录发送给客户端文件主体内容大小;
$http_referer:用来记录从哪个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。

location常见配置指令, root, alias, proxy_pass
root (根路径配置) :请求www.gcc.com/test/1.jpg,会返回文件/usr/local/nginx/html/test/1.jpg
alias (别名配置) :请求www.gcc.com/test/1.jpg,会返回文件/usr/local/nginx/html/1.jpg

proxypass (反向代理配置)
proxy_pass http://127.0.0.1:8080/;  会转发请求到http://127.0.0.1:8080/1.jpg
proxy_ pass http://127.0.0.1:8080;  会转发请求到http://127.0.0.1:8080/test/1.jpg
vim /usr/local/nginx/conf/nginx.conf             ##nginx主配置文件

在这里插入图片描述

[root@www init.d]# cd /usr/local/nginx/html/
[root@www html]# ls
50x.html  index.html
[root@www html]# mkdir test
[root@localhost test]#vim index.html
   this is test!
[root@www html]# systemctl restart nginx.service

在这里插入图片描述


但这个时候我们在到Nginx的主配置文件中,将root 改成alias,结果又会不一样


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述



设置临时域名解析

echo "192.168.121.12 www.song.com" >> /etc/hosts
systemctl restart nginx.service

在这里插入图片描述

在这里插入图片描述



访问状态统计配置

  • 1、先使用命令/usr/local/nginx/sbin/nginx -V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块
  • 2、修改 nginx.conf 配置文件,指定访问位置并添加 stub_status 配置
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
 server {
  listen 80;
  server_name www.lic.com;
  charset utf-8;
  location / {
   root html;
   index index.html index.php;
  }
  ##添加 stub_status 配置##
  location /status {      #访问位置为/status
   stub_status on;     #打开状态统计功能
   access_log off;     #关闭此位置的日志记录
  }
 }
}

在这里插入图片描述



基于授权的访问控制



1、生成用户密码认证文件

yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db zhangsan
chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db



2、修改主配置文件相对应目录,添加认证配置项

vim /usr/local/nginx/conf/nginx.conf
......
 server {
  location / {
   ......
   ##添加认证配置##
   auth_basic "secret";
   auth_basic_user_file /usr/local/nginx/passwd.db;
  }
 }



3、重启服务,访问测试

nginx -t
systemctl restart nginx

浏览器访问 http://192.168.121.12或www.song.com

在这里插入图片描述



基于客户端的访问控制


访问控制规则如下



deny IP/IP段:拒绝某个 IP 或 IP段的客户端访问

allow IP/IP段:允许某个 IP 或IP 段的客户端的访问

规则从上往下执行,如匹配则停止,不再往下匹配

vim /usr/local/nginx/conf/nginx.conf
 location / {
            root   html;
            index  index.html index.htm;
            deny 192.168.121.12;      #添加拒绝访问的客户端的IP
            allow all;        #添加允许其他IP客户端访问
        }

在这里插入图片描述



基于域名的 Nginx 虚拟主机



1、为虚拟主机提供域名解析

echo "192.168.121.12 www.song.com www.accp.com" >> /etc/hosts           ##Linux系统
C/Windows/system32/drivers/etc/hosts                ##Windows系统到这个文件位置去修改



2、为虚拟主机准备网页文档

mkdir -p /var/www/html/song
mkdir -p /var/www/html/accp
echo "<h1> www.song.com</h1>" >/var/www/html/song/index.html
echo "<h1> www.accp.com</h1>" >/var/www/html/accp/index.html



3、修改 Nginx的配置文件


vim /usr/local/nginx/conf/nginx.conf
……
http {
……
 server {
  listen 80
  server_name www.song.com;———————————设置域名www.song.com
  charset utf-8;
  access_log logs/www.song.access.log;————————————设置日志名
  location / {
   root /var/www/html/song;——————————————设置www.song.com的工作目录
   index index.html index.php;
  }
  error_page 500 502 503 504 /50x.html;
  location = 50x.html{
    root html;
  }
 }
 
 server {
  listen 80
  server_name www.accp.com;————————————设置域名www.accp.com
  charset utf-8;
  access_log logs/www.accp.access.log;
  location / {
   root /var/www/html/accp;
   index index.html index.php;
  }
  error_page 500 502 503 504 /50x.html;
  location = 50x.html{
    root html;
  }
 }
}

nginx -t  #检查语法
systemctl restart nginx

浏览器访问:
http://www.accp.com
http://www.song.com



基于端口的虚拟主机



1、修改nginx主配置文件,仅修改监听端口

vim /usr/local/nginx/conf/nginx.conf
……
http {
……
 server {
  listen 192.168.121.12:888;——————————设置监听888端口
  server_name www.accp.com;
  charset utf-8;
  access_log logs/www.accp.access.log;
  location / {
   root /var/www/html/accp;
   index index.html index.php;
  }
  error_page 500 502 503 504 /50x.html;
  location = 50x.html{
    root html;
  }
 }
 server {
  listen 192.168.121.12:666;——————————设置监听666端口
  server_name www.song.com;
  charset utf-8;
  access_log logs/www.song.access.log;
  location / {
   root /var/www/html/song;
   index index.html index.php;
  }
  error_page 500 502 503 504 /50x.html;
  location = 50x.html{
    root html;
  }
 }
}

nginx -t  #检查语法
systemctl restart nginx


浏览器访问
http://192.168.121.12666
http://192.168.121.12888

在这里插入图片描述

在这里插入图片描述



基于不同IP的虚拟主机



1、添加网卡,修改域名、IP

ifconfig ens33:0 192.168.121.100 netmask 255.255.255.0
--------------------------------------------------------
vim /usr/local/nginx/conf/nginx.conf
……
http {
……
 server {
  listen 192.168.121.12:80;——————————设置监听地址192.168.121.12
  server_name www.song.com;
  charset utf-8;
  access_log logs/www.gcc.access.log;
  location / {
   root /var/www/html/gcc;
   index index.html index.php;
  }
  error_page 500 502 503 504 /50x.html;
  location = 50x.html{
    root html;
  }
 }
 server {
  listen 192.168.200.100:80;——————————设置监听地址192.168.200.100
  server_name www.benet.com;
  charset utf-8;
  access_log logs/www.benet.access.log;
  location / {
   root /var/www/html/benet;
   index index.html index.php;
  }
  error_page 500 502 503 504 /50x.html;
  location = 50x.html{
    root html;
  }
 }
}


nginx -t
systemctl restart nginx


http://192.168.200.50
http://192.168.200.100

在这里插入图片描述

在这里插入图片描述



版权声明:本文为m0_54585768原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。