• nginx基础整理


    目录结构如下:

        Nginx基础知识

            Nginx HTTP服务器的特色及优点

            Nginx的主要企业功能

            Nginx作为web服务器的主要应用场景包括:  

        Nginx的安装

            安装环境

            快速安装命令集合

            各个命令解释

            脚本

            注意

        安装故障总结

            故障一:没有安装pcre或pcre-devel

            故障二:没有安装openssl和openssl-devel

        常用的Nginx http功能模块

        Nginx的目录结构

        Nginx最重要的配置文件nginx.conf详解

        生产中常见的网站状态码

    Nginx基础知识:

    Nginx HTTP服务器的特色及优点

    a. 支持高并发:能支持几万并发连接(特别是静态小文件业务环境)

    b. 资源消耗少:在3万并发连接下,开启10个Nginx线程消耗的内存不到200MB

    c. 可以做HTTP反向代理及加速缓存,即负载均衡功能,内置对RS节点服务器健康检查功能,这相当于专业的Haproxy软件或LVS的功能

    d. 具备Squid等专业缓存软件等的缓存功能

    e. 支持异步网络I/O事件模型epoll

    Nginx的主要企业功能

    a. 使用Nginx运行HTML,JS,CSS,小图片等静态数据(此功能类似Lighttpd软件)

    b. Nginx结合FastCGI运行php等动态程序(例如使用fastcgi_pass方式)

    c. Nginx结合Tomcat/Resin等支持Java动态程序(常用的proxy_pass)

    Nginx作为web服务器的主要应用场景包括:

    a. 使用Nginx运行HTML,JS,CSS,小图片等静态数据(此功能类似Lighttpd软件)

    b. Nginx结合FastCGI运行php等动态程序(例如使用fastcgi_pass方式)

    c. Nginx结合Tomcat/Resin等支持Java动态程序(常用的proxy_pass)

    一般情况下普通php引擎支持的并发连接参考为300-1000,Java引擎和数据库的并发连接参考值为300-1500.当然架构不同可能会有浮动

    Nginx的安装

    安装环境

    a. 查看当前系统cat /etc/redhat-release

    [root@nginx /]# cat /etc/redhat-release

    CentOS release 6.7 (Final)

    [root@nginx /]#

    b. 查看系统内核uname –r

    [root@nginx /]# uname -r

    2.6.32-573.el6.x86_64

    [root@nginx /]#

    快速安装命令集合:

    复制代码
    1 yum install pcre pcre-devel –y
    2 yum install openssl openssl-devel –y
    3 useradd nginx -M -s /sbin/nologin
    4 ./configure --user=nginx --group=nginx --prefix=/application/nginx1.6.2  --with-http_stub_status_module  --with-http_ssl_module
    5 make&&make install
    6 ln -s /application/nginx1.6.2/ /application/nginx
    7 /application/nginx/sbin/nginx –t
    8 /application/nginx/sbin/nginx
    复制代码

    各个命令解释

    a. 安装前需要安装pcre库(兼容正则表达式)

    yum install pcre pcre-devel –y

    b. 还需要安装openssl

    yum install openssl openssl-devel –y

    c. 编译之前还需要创建一个用户

    useradd nginx -M -s /sbin/nologin

    d. 编译安装:

    ./configure --user=nginx --group=nginx --prefix=/application/nginx1.6.2  --with-http_stub_status_module  --with-http_ssl_module

    make&&make install

    e. 安装完成后的检查与启动

    /application/nginx/sbin/nginx –t

    /application/nginx/sbin/nginx

    脚本

    a. 同样的可以通过脚本实现整体的安装(脚本如下)

     View Code

    注意

    a. 如果是学习,需要关闭防火墙和selinux,关闭方法如下:

    /etc/init.d/iptables stop

    setenforce 0临时关闭)

    b. 如果想要永久关闭selinux

    vi编辑/etc/selinux/config进行下面更改

    SELINUX=disabled

    c. 也可以通过命令sed直接对命令进行修改

    sed -i 's#SELINUX=enable#SELINUX=disabled#g' /etc/selinux/config

    按照上述操作启动成功后,通过浏览器打开访问:

    安装故障总结

    故障一:没有安装pcre或pcre-devel

    会提示如下错误:

    1 ./configure: error: the HTTP rewrite module requires the PCRE library.
    2 You can either disable the module by using --without-http_rewrite_module
    3 option, or install the PCRE library into the system, or build the PCRE library
    4 statically from the source with nginx by using --with-pcre=<path> option.

    故障二:没有安装openssl和openssl-devel

    1 ./configure: error: SSL modules require the OpenSSL library.
    2 You can either do not enable the modules, or install the OpenSSL library
    3 into the system, or build the OpenSSL library statically from the source
    4 with nginx by using --with-openssl=<path> option.

    常用的Nginx http功能模块

    Nginx http功能模块

    模块说明

    Ngx_http_core_module

    包括一些核心的http参数配置,对应Nginx的配合为HTTP区块部分

    Ngx_http_access_module

    访问控制模块,用来控制网站用户对Nginx的访问

    Ngx_http_gzip_module

    压缩模块,对Nginx返回的数据压缩,属于性能优化模块

    Ngx_http_fastcgi_module

    FastCGI模块,和动态应用相关的模块,例如PHP

    Ngx_http_proxy_module

    Proxy 代理模块

    Ngx_http_upstream_module

    负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查

    Ngx_http_rewrite_module

    URL地址重写模块

    Ngx_http_limit_conn_module

    限制用户并发连接数及请求数模块

    Ngx_http_limit_req_module

    根据定义的key限制Nginx请求过程的速率

    Ngx_http_log_module

    访问日志模块,以指定的格式记录Nginx客户访问日志等信息

    Ngx_http_auth_basic_module

    web认证模块,设置web用户通过账号,密码访问Nginx

    Ngx_http_ssl_module

    ssl模块,用于加密的http连接如https

    Ngx_http_stub_status_module

    记录Nginx基本访问状态信息等的模块

    Nginx的目录结构

    |-- client_body_temp

    |-- conf                            #这是Nginx所有配置文件的目录

    |   |-- fastcgi.conf                 #fastcgi相关参数的配置文件

    |   |-- fastcgi.conf.default   

    |   |-- fastcgi_params               #fastcgi的参数文件

    |   |-- fastcgi_params.default

    |   |-- koi-utf

    |   |-- koi-win

    |   |-- mime.types                #媒体类型

    |   |-- mime.types.default  

    |   |-- nginx.conf                  #nginx默认的主配置文件

    |   |-- nginx.conf.default

    |   |-- scgi_params               #scgi相关参数

    |   |-- scgi_params.default

    |   |-- uwsgi_params             #uwsgi相关参数

    |   |-- uwsgi_params.default

    |   `-- win-utf

    |-- fastcgi_temp                 #fastcgi临时数据目录

    |-- html                              #编译安装Nginx的默认站点目录

    |   |-- 50x.html                   #错误页面优雅替代显示文件

    |   `-- index.html               #默认的首页文件

    |-- logs                             #默认的日志路径包括错误日志和访问日志

    |   |-- access.log

    |   |-- error.log

    |   `-- nginx.pid

    |-- proxy_temp                  #临时目录

    |-- sbin                             Nginx命令目录

    |   `-- nginx                     启动命令

    |-- scgi_temp                   #临时目录

    `-- uwsgi_temp                  #临时目录

    Nginx最重要的配置文件nginx.conf详解

    通过命令将nginx配置文件精简化显示(去掉#注释和空行的内容):

    egrep -v "#|^$" nginx.conf.default >nginx.conf

    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   500 502 503 504  /50x.html; #出现对象http状态码时使用50x.html回应用户

            location = /50x.html {

                root   html;

            }

        }

    }                                 #http区块结束

    生产中常见的网站状态码

    状态码

    详细描述说明

    200-OK

    服务器成功返回网页,这是成功的状态码

    301-Moved Permanently

    永久跳转,所请求的网页将永久跳转到被设定的新位置

    403-Forbidden

    禁止访问,虽然这个请求时合法的,但是服务器端因为匹配了预先设置的规则而拒绝相应客户端的请求,此类问题一般为服务器或服务器权限配置不当所致

    404-Not Found

    服务器找不到客户端请求的指定页面,可能是客户端请求了服务器上不存在的资源所导致

    500-Internal Server Error

    内部服务器错误,服务器遇到了意料不到的情况,不能完成客户的请求,这是一个较为笼统的报错,一般为服务器的设置或内部程序问题导致

    502-Bad Gateway

    坏的网关,一般是代理服务器请求后端服务时,后端服务不可用或没有完成相应网关服务器,这通常为反向代理服务器下面的节点出问题导致

    503-Service Unavailable

    服务当前不可用,可能是服务器超载或停机维护导致的,或者是反向代理没有可以提供的服务节点

    504-Gateway Timeout

    网关超时,一般是网关代理服务器请求后端服务时,后端服务没有在特定的时间内完成处理请求,多数是服务器过载导致没有在指定的时间内返回数据给前端代理服务器

  • 相关阅读:
    牛客-小V和gcd树
    CF-Codeforces Round #639 (Div. 2)-D-Monopole Magnets
    牛客-牛牛染颜色
    CF-637
    提高办公效率——快捷键
    FusionComputer虚拟机克隆后出现网卡服务重启失败解决办法
    centos7.5mini中Firewalld端口不通解决办法及想法
    FusionConputer热迁移过程记录
    Java集合框架之Iterable接口
    初识Java集合框架
  • 原文地址:https://www.cnblogs.com/guigujun/p/6588545.html
Copyright © 2020-2023  润新知