• nginx理论


    什么是nginx

    nginx 是一个开源且高性能, 可靠的Http web服务, 代理服务, 负载均衡

    开源: 直接获取源代码

    高性能: 高并发

    可靠: 服务稳定

    轻量: 占用资源少


    常见的HTTP Web服务

    httpd 由apache基金会

    IIS 微软服务器版

    GWS google开发

    Openrestry 基于nginx + lua

    Tengline 淘宝基于nginx开发


    市场占有率: apache>IIS>nginx

    nginx 在互联网行业使用比较广泛


    为什么选择nginx

    Nginx非常轻量

    功能模块少(源代码仅保留http与核心模块代码, 其余不够核心代码会作为插件来安装)

    代码模块化 (易读, 便于二次开发, 对于开发人员非常友好)

    server {
    	
    	location /nba {
    	
    	}
    	
    	location /cba {
    	
    	}
    
    }
    
    server {
    	
    	location /lxx {
    	
    	}
    	
    	location /lyy {
    	
    	}
    
    }
    

    nginx 适合做微服务, 云架构, 中间层


    nginx 采用Epool网络模型, apache 采用select模型

    • select
      1. 当用户发起一次请求, 会存入FD(文件描述符),
      2. select模型每次调用就会进行一次遍历扫描FD文件描述符,默认值是1024/2048,
      3. 如果修改这个值, 会影响整体性能, 使得很多连接超时,
      4. 数据由内核拷贝到用户态
    • Epool
      1. Epoll 没有最大并发连接的限制,上限是最大可以打开文件的数目,这个数字一般远大于 2048, 一般来说这个数目和系统内存关系很大
      2. Epoll 不仅会告诉应用程序有I/0 事件到来,还会告诉应用程序相关的信息,这些信息是应用程序填充的,因此根据这些信息应用程序就能直接定位到事件,而不必遍历整个FD 集合

    https://blog.csdn.net/foxgod/article/details/93165645


    nignx 应用场景

    静态服务

    • 浏览器缓存
    • 防资源调用(反爬)
    • 资源分类
    • 资源压缩
    • 资源缓存
    • 跨域访问

    代理服务

    代理的优点就是可以缓存,流程为PC把请求发送给代理服务器, 代理服务器访问到了服务器, 缓存下来, 再提供给PC, 当PC下次再访问, 则更快

    代理服务器可以限制访问页面大小(缓存大小), 屏蔽敏感词汇

    https://www.cnblogs.com/xuepei/p/10437114.html


    • 正向代理(客户端代理)

      "代理服务器"代理了"客户端",去和"目标服务器"进行交互, 隐藏客户端的IP, web服务器上只能看到代理服务器的IP


    • 反向代理(服务器代理)

      外访内,是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器

      使用场景就是负载均衡, 当受到攻击时候, 暴露的IP是负载均衡的IP, web服务器不会受影响


    • 代理缓存
    • 动静分离

    安全服务

    • 访问控制

      基于身份认证的


    • WAF (防攻击, DDOS, CC, SQL注入)
      • 流量限制
      • 拦截攻击
      • 拦截异常请求
      • 拦截SQL注入

    流行架构

    • Nginx + PHP(Fastcgi_pass)LNMP
    • Nginx + Jave(Proxy_Pass)LNMT
    • Nginx + Python (uwsgi_pass)

    nginx安装

    http://nginx.org/en/linux_packages.html#RHEL-CentOS

    • 配置yum仓库
    $ yum install yum-utils
    $ vim /etc/yum.repos.d/nginx.repo
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true
    $ yum install nginx -y
    

    $ nginx -v
    nginx version: nginx/1.16.1
    

    nginx配置


    查看nginx的文件

    $ rpm -ql nginx
    

    nginx 主配置文件

    /etc/nginx
    /etc/nginx/nginx.conf
    /etc/nginx/conf.d
    /etc/nginx/conf.d/default.conf
    

    cgi, Fastcgi, uwsgi配置文件

    /etc/nginx/uwsgi_params
    /etc/nginx/scgi_params
    /etc/nginx/fastcgi_params
    

    nginx编码转换映射文件

    /etc/nginx/koi-utf
    /etc/nginx/koi-win
    /etc/nginx/win-utf
    

    http协议的Content-Type 与扩展名

    /etc/nginx/mime.types
    

    配置文件守护进程管理器

    /usr/lib/systemd/system/nginx.service
    -----> systemctl reload nginx
    

    nginx日志轮询, 日志切割

    /etc/logrotate.d/nginx
    

    nginx终端管理命令

    /usr/sbin/nginx
    -----> nginx -s reload
    /usr/sbin/nginx-debug
    /usr/lib64/nginx
    /usr/lib64/nginx/modules
    

    nginx默认站点目录

    /usr/share/nginx
    /usr/share/nginx/html
    /usr/share/nginx/html/50x.html
    /usr/share/nginx/html/index.html
    

    nginx的帮助手册

    /usr/share/doc/nginx-1.16.1
    /usr/share/doc/nginx-1.16.1/COPYRIGHT
    /usr/share/man/man8/nginx.8.gz
    
    

    nginx的缓存目录

    /var/cache/nginx
    
    

    nginx的日志记录

    /var/log/nginx
    

    查看nginx安装的选项

    $ nginx -V 2>&1 |grep 'prefix'|cut -d: -f2
    
    --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
    

    编译选项

    • 程序安装目录和路径
    --prefix=/etc/nginx 
    --sbin-path=/usr/sbin/nginx 
    --modules-path=/usr/lib64/nginx/modules 
    --conf-path=/etc/nginx/nginx.conf 
    --error-log-path=/var/log/nginx/error.log 
    --http-log-path=/var/log/nginx/access.log 
    --pid-path=/var/run/nginx.pid 
    --lock-path=/var/run/nginx.lock
    

    • 临时缓存文件
    --http-client-body-temp-path=/var/cache/nginx/client_temp 
    --http-proxy-temp-path=/var/cache/nginx/proxy_temp 
    --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp 
    --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp 
    --http-scgi-temp-path=/var/cache/nginx/scgi_temp
    

    • 设定nginx进程启动用户和组(安全)
    --user=nginx 
    --group=nginx
    
    

    • 设置额外的参数将被添加到CFLAGS变量
    --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' 
    
    

    • 设置附加的参数, 链接系统库
    --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
    

    场景

    • 如果之前的nignx通过源码安装, 这个时候部署新的nginx服务器, 如何部署

      通过 nginx -v 获得版本

      通过nginx -V 获得configure选项


    nginx配置文件

    user  nginx;
    worker_processes  1;
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    events {
        worker_connections  1024;
    }
    http {
        include       /etc/nginx/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  /var/log/nginx/access.log  main;
        sendfile        on;
        keepalive_timeout  65;
        include /etc/nginx/conf.d/*.conf;
    }
    

    coreModule

    核心模块, 全局配置, 可以有events, http

    user  nginx;   # nginx进程所使用的用户
    worker_processes  1;   # nginx运行的work进程数量(建议和CPU数量一直或auto)
    
    error_log  /var/log/nginx/error.log warn;   # nginx错误日志存放路径
    pid        /var/run/nginx.pid;   # nginx服务运行后产生的pid进程号
    

    EventModule

    事件驱动模块

    events {
        worker_connections  1024;   # 每个worker进程支持的最大连接数
        use epool;    # 事件驱动模型, epoll默认
    }
    

    HttpCoreModule Http

    内核模块

    http模块曾允许有多个server曾, server主要用于配置多个网站

    server层允许多个Location, Location主要用于定义网站访问路径

    http {
        include       /etc/nginx/mime.types;   # html渲染所支持的样式
        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  /var/log/nginx/access.log  main;
    	
    	
        sendfile        on;		# 是否能发送文件
        #tcp_nopush     on;
    
        keepalive_timeout  65;   # 长连接超时时间65s
    
        #gzip  on;	# 是否压缩
    
        include /etc/nginx/conf.d/*.conf;	# 模块导入路径
        
        # 使用server模块来配置网站, 每个server{} 代表一个网站(简称虚拟主机)
        server {
        	listen 80;	#监听端口
        	server_name: localhost;	# 提供服务器的域名或主机名
        	access_log host.access.log;   # 访问日志
        	
        	# 控制网站访问路径
        	location / {
        		root	/usr/share/nginx/html;   # 存放网站代码路径
        		index	index.html index.htm;	# 服务器返回的默认页面文件
        	}
        	
        	# 指定错误代码, 统一定义错误页面, 错误代码重定向到新的Location
        	error_page	500 502 503 504	/50x.html;
        }
        
        }
    

    nginx配置网站

    当我们访问game.weixinyu.com的时候, 访问/weixinyu_code/里面的页面代码, 并且响应头要有business和author这两个字段

    • 配置server模块
    $ vim /etc/nginx/conf.d/weixinyu.conf 
    server {
        listen       80;
        server_name  game.weixinyu.com;
    
    
        location / {
            root   /weixinyu_code;
            index  index.html index.htm;
            add_header business ops;
            add_header authors weixinyu;
        }
    
    }
    

    • 检查nginx配置文件语法
    $ nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    

    • 创建目录
    $ mkdir /weixinyu_code
    

    • 关闭default.conf
    $ mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.off
    

    • 关闭防火墙和selinux

    • 放入代码
    echo 'weixinyu' > /weixinyu_code/index.html
    

    • 本地hosts文件做本地DNS解析 C:WindowsSystem32driversetchosts

      需要修改文件权限, 右击属性-安全-Users, 全部允许

    192.168.221.20  game.weixinyu.com
    

    • 重启nginx, 重新载入nginx
    $ systemctl restart nginx
    $ nginx -s reload
    

    • 查看80端口
    $ lsof -i:80
    COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    nginx   9921  root    6u  IPv4  41141      0t0  TCP *:http (LISTEN)
    nginx   9923 nginx    6u  IPv4  41141      0t0  TCP *:http (LISTEN)
    

  • 相关阅读:
    第十六周个人作业
    小组作业
    第十五周个人作业
    本周个人总结
    产品计划会议
    本周个人总结
    排球计分程序
    JProfiler入门
    (转)MMOGS服务器架构设计
    (转)游戏服务器开发需要学习的技术
  • 原文地址:https://www.cnblogs.com/cjwnb/p/11980557.html
Copyright © 2020-2023  润新知