Nginx网络服务

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

目录

1.Nginx基础

1.Nginx和Apache的差异

2.Nginx和Apache的优缺点比较

3.编译安装nginx服务

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

1.全局配置

2.I/O事件配置

3.HTTP设置

4.访问状态统计配置

5.基于授权密码的访问控制

6.基于客户端的访问控制

7.基于域名的nginx 虚拟主机

8.基于IP地址

9.基于端口


1.Nginx基础

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

     2稳定性高

     3系统资源消耗低

     4对HTTP并发连接的处理能力高

     5单台物理服务器可支持30 000~50 000个并发请求。

1.Nginx和Apache的差异

轻量级Nginxt比Apache占用更少的内存及资源
静态处理Nginx静态处理性能比Apache高
Nginx可以实现无缓存的反向代理加速提高网站运行速度
Nginx支持热部署启动速度迅速可以在不间断服务的情况下对软件版本或者配置进行升级
Nginx高度模块化编写模块相对简单且组件比Apache少
高并发下Nginx能保持低资源低消耗高性能
Nginx是异步进程多个连接可以对应一个进程;Apache是同步多进程一个连接对应一个进程
Nginx的性能和可伸缩性不依赖于硬件Apache依赖于硬件
Nginx配置简洁Apache配置复杂。

  2.Nginx和Apache的优缺点比较

1nginx相对于apache的优点∶

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

2apache相对于nginx的优点∶

 rewrite比nginx的rewrite强大模块多基本想到的都可以找到少bug Nginx的bug相对较超稳定存在就是理由一般来说需要性能的web服务用Nginx。如果不需要性能只求稳定那就Apache。Nginx处理动态请求是弱项一般动态请求要Apache去做Nginx只适处理静态网页或反向代理。

3.编译安装nginx服务

   1.关闭防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0

2.安装依赖包

     nginx的配置及运行需要pcre、zlib等软件包的支持因此需要安装这些软件的开发包以便提供相应的库和头文件。

[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make

3.创建运行用户、组

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

useradd -M -s /sbin/nologin nginx

4.将安装nginx所需的软件包传到/opt目录下并解压

cd /opt/
wget http://nginx.org/download/nginx-1.18.0.tar.gz
#官网下载安装包

5.编译安装Nginx

配置

tar xf nginx-1.18.0.tar.gz 
cd nginx-1.18.0/
#解压软件包
mkdir /apps/nginx -p


./configure --help   
#查看帮助模块

./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module

 编译安装

[root@localhost nginx-1.18.0]# make && make install

6.修改权限和生成四个文件

chown -R nginx.nginx /apps/nginx
#修改权限

ll /apps/nginx/
total 0
drwxr-xr-x 2 root root 333 Sep 22 12:49 conf
drwxr-xr-x 2 root root  40 Sep 22 12:49 html
drwxr-xr-x 2 root root   6 Sep 22 12:49 logs
drwxr-xr-x 2 root root  19 Sep 22 12:49 sbin

######安装好后生成四个文件

7.优化路径

[root@localhost local]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
创建软连接 方便系统识别

8. 检查、启动、重启、停止Nginx服务

nginx -t								#检查配置文件是否配置正确
nginx							        #启动		
#停止
cat /usr/local/nginx/logs/nginx.pid		#先查看nginx的PID号
kill -3 <PID号>
或者				    
killall -3 nginx
killall -s QUIT nginx
------------------------------------------------
#重载
kill -1 <PID号>					
kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx
-----------------------------------------------
#日志分隔重新打开日志文件
kill -USR1 <PID号>
-----------------------------------------------
#平滑升级
kill -USR2 <PID号>
------------------------------------------------
新版本升级
tar -zxvf nginx-1.xx.xx.tar.gz
cd nginx-1.xx.xx
./configure \
-prefix=/usr/local/ngink \
-user=nginx\
-group=nginx \
-with-http stub_status _module\
-with-http_ssl_module
 
make
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old
cp objs/nginx /usr/local/nginx/sbin/nginx
make upgrade
#或者先killall nginx再/usr/local/nginx/sbin/nginx

9.添加 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
 
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
-----------------------------------------------------
[Unit]:服务的说明
Description :描述服务
After:依赖,当依赖的服务启动之后再启动自定义的服务
 
[service]服务运行参数的i设置
Type=forking是后台运行的形式使用此启动类型应同时指定
PIDFile=以便systemd能够跟踪服务的主进程。Execstart为服务的具体运行命令
ExecReload为重启命令
Execstop为停止命令
PrivateTrmp=True丧示给服务分配独立.的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径
 
[Insta11]服务安装的相关设置,可设置为多用户

 

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

配置文件包含的内容

全局块全局配置对全局生效
events块配置影响Nginx服务器与用户的网络连接
http块配置代理缓存日志定义等绝大多数功能和第三方模块的配置
server块配置虚拟主机的相关参数一个http块中可以有多个server块
location块用于配置匹配的uri
upstream配置后端服务器具体地址负载均衡配置不可或缺的部分。

vim /apps/nginx/conf/nginx.conf 

 

1.全局配置

#user nobody;     #运行用户若编译时未指定则默认为 nobody
worker_ processes 1;   #工作运行数量一般设置为和CPU核数一样
#error_log logs/error.log;  #错误日志文件的位置
#pid logs/nginx.pid;   #PID文件的位置

 

2.I/O事件配置

  

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

允许 Nginx 正常提供服务的连接数并发量工作进程数为 1每个进程处理 1024 个连接则为cpu核数*连接数1*1024=1024

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

4.epollsocket描述符是Linux内核为处理大批量文件描述符而作了改进的poll是Linux下多路复用IO接口select/poll的增强版本它能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率.

 内核优化

vim /etc/security/limits.conf 

 

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;
 	##减少网络报文段的数量
    #tcp_nopush     on;
	##连接保持超时时间单位是秒
    #keepalive_timeout  0;  #不开启长连接
    keepalive_timeout  65;
	##gzip模块设置设置是否开启gzip压缩输出
    #gzip  on;
##Web 服务的监听配置
server {
	##监听地址及端口
	listen 80; 
	##站点域名可以有多个用空格隔开
	server_name localhost;
	##网页的默认字符集
	charset utf-8;
	##根目录配置
	location / {
	
		##网站根目录的位置/usr/local/nginx/html
		root 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地址; $remote user∶ 用来记录客户端用户名称; $time local∶ 用来记录访问时间与时区;$request∶用来记录请求的url与http协议; $status∶ 用来记录请求状态;成功是200 $body bytes sent ∶ 记录发送给客户端文件主体内容大小; $http referer∶ 用来记录从哪个页面链接访问过来的; $http user agent∶记录客户浏览器的相关信息;

通常web服务器放在反向代理的后面这样就不能获取到客户的IP地址了通过Sremote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中可以增加x_forwarded_for信息用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。

location常见配置指令 root、alias、proxy_ pass root 根路径配置∶ 请求ww.clj.com/test/1.jpg会返回文件/usr/local/nginx/html/test/1.jpg alias 别名配置∶请求www.clj.com/test/1.jpg会返回文件/usr/local/nginx/html/1.jpg proxy_pass 反向代理配置∶ 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

4.访问状态统计配置

      1.nginx 内置了 HTTP_STUB_STATUS 状态统计模块用来反馈当前的 Web 访问情况 配置编译参数时可添加--with-http_stub_status_module 来启用此模块支持可以使用命令

     2.可以使用命令/usr/local/nginx/sbin/nginx –v 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块。

3.操作步骤:

  (1)修改 nginx.conf 配置文件指定访问位置并添加 stub_status 配置修改之前进行备份

#先拷贝一份配置文件
[root@localhost conf]#cp /usr/local/nginx/conf/nginx.conf nginx.conf.bak
 
#  修改 nginx.conf 配置文件
[root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf
 
events {
    use epoll;
    worker_connections  1024;
}
 
    server {
        listen       80;
        server_name  www.yxp.com;
 
 
        charset utf-8;
      location / {
            root   html;
            index  index.html index.htm;
        }
 
        location /status {  ##访问位置为/status
            stub_status on;  ##打开状态统计功能
            access_log off;  ##关闭此位置的日志记录
        }

 

 

(2)在网页中输入 192.168.156.10/status 测试

Active connections: 2 
server accepts handled requests
 2 2 4 
Reading: 0 Writing: 1 Waiting: 1 

Active connections: 2 #当前活动链接数 server accepts handled requests 6 6 5 #表示已经处理的连接信息三个数字一次表示已处理的连接数、成功的TCP握手次数、已处理的请求数 Reading: 0 Writing: 1 Waiting: 1

(3)结合awk来写shell脚本如果链接数过高报警

#/bin/bash
a=$(curl 192.168.156.10/status)
b=$(echo $a|awk '{print $3}')
if [ $b -ge 1000 ]
then 
echo "连接数过高"|mail -s test 2094113933@qq.com
else
echo "运行良好"
fi

5.基于授权密码的访问控制

 (1) 生成用户密码认证文件

#安装工具
[root@localhost conf]#yum install -y httpd-tools.x86_64 
 
#如果没有密码则设置
[root@localhost conf]#htpasswd -c /usr/local/nginx/passwd.db zhangsan
#第二次不用加-c
[root@localhost conf]#htpasswd /usr/local/nginx/passwd.db lisi
[root@localhost conf]#chown nginx /usr/local/nginx/passwd.db
[root@localhost conf]#chmod 400 /usr/local/nginx/passwd.db

 

(2修改主配置文件相应的目录

        location /status { 
           #添加密码认证
           auth_basic "secret";
           auth_basic_user_file /usr/local/nginx/passwd.db;
           
           # stub_status on;
           # access_log off;
            root   html;
            index  index.html index.htm;
        }

 

3重启服务

[root@localhost conf]#nginx -t
[root@localhost conf]#systemctl restart nginx.service 

4在网页测试

http//192.168.156.10需要输入账号和设置的密码才能登录

6.基于客户端的访问控制

1.基于客户端的访问控制简介 基于客户端的访问控制是通过客户端 IP 地址决定是否允许对页面访问。Nginx 基于 客户端的访问控制要比 Apache 简单规则如下 1deny IP/IP 段拒绝某个 IP 或 IP 段的客户端访问。 2allow IP/IP 段允许某个 IP 或 IP 段的客户端访问。 3规则从上往下执行如匹配则停止不再往下匹配。

1修改配置文件

[root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf
  location / {
            #auth_basic "secret";
            #auth_basic_user_file /usr/local/nginx/passwd.db;
            deny 192.168.156.110;                      ###客户端IP
            allow all;
            root   html;
            index  index.html index.htm;
        }

 

2) 重启服务,网页测试

 

 

7.基于域名的nginx 虚拟主机

利用虚拟主机不用为每个要运行的网站提供一台单独的 Nginx 服务器或单独运行一 组 Nginx 进程虚拟主机提供了在同一台服务器同一组 Nginx 进程上运行多个网站的功 能。跟 Apache 一样Nginx 也可以配置多种类型的虚拟主机分别是基于 IP 的虚拟主机、 基于域名的虚拟主机、基于端口的虚拟主机。 使用 Nginx 搭建虚拟主机服务器时每个虚拟 Web 站点拥有独立的“server{}”配置段 各自监听的 IP 地址、端口号可以单独指定当然网站名称也是不同的。

(1)为虚拟主机创建文档

[root@localhost html]#mkdir -p /var/www/html/{yxp,accp}
[root@localhost html]#echo "<h1>www.yxp.com</h1>" > /var/www/html/yxp/index.html
[root@localhost html]#echo "<h1>www.accp.com</h1>" > /var/www/html/accp/index.html

(2) 修改配置文件

vim /usr/local/nginx/conf/nginx.conf
 
    server {
        listen       80;
        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.htm;
        }
 
        #error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
 
    server {
        listen       80;
        server_name  www.yxp.com;
 
        charset utf-8;
 
        access_log  logs/yxp.access.log;
 
        location / {
            root   /var/www/html/yxp;
            index  index.html index.htm;
        }
 
        #error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

 

 

(3)重启服务并测试

此时域名无法使用需要修改/etc/hosts配置文件

C:\Windows\System32\drivers\etc\hosts

 

 

 

8.基于IP地址

  1.修改配置文件

 

[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf
server {
        listen   192.168.156.10:80;
        server_name  www.yxp.com;
        
        
server {
        listen   192.168.156.110:80;
        server_name  www.accp.com;

9.基于端口

[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf
server {
        listen   192.168.156.10:80;
        server_name  www.yxp.com;
        
        
server {
        listen   192.168.156.110:8080;
        server_name  www.accp.com;

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: nginx

“Nginx网络服务” 的相关文章