nginx入门

1.Nginx基本简述
Nginx是一个开源且高性能、可靠的http web服务、代理服务、负载均衡。

开源:直接获取源代码
高性能:支持海量并发
可靠:服务稳定
轻量:占用资源少

1).常见的HTTP Web服务

Http 由Apache基金会
IIS 微软服务器版
GWS Google开发
Openrestry 基于nginx+lua
Tengline 淘宝基于Nginx开发

2).为什么选择 Nginx

Nginx非常轻量
    功能模块少(源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装)
    代码模块化(易读,便于二次开发,对开发人员非常友好)
    互联网公司都选择Nginx

Nginx技术成熟,国内公司基本大规模使用
    适合当前主流架构趋势,微服务、云架构、中间层
    统一技术栈,降低维护成本,降低技术更新成本。
    
Nginx采用Epool网络模型,Apache采用Select模型
    Select:当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致扫描低下。
    Eopoll:当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。

3).Nginx应用场景

    静态服务
        html|jpg|png|...
        Nginx缓存
    代理服务
        正向代理,内访外
        反向代理,外访内,和负载均衡的效果是一样的,但是工作方式不一样
    负载均衡    
    安全服务
        访问控制
            基于IP的
            基于身份认证的
        WAF
            云waf
            硬件waf
                DDOS攻击
                CC攻击
                SQL注入
                ...
            nginx+lua ?
            花钱买       ?    
    架构
        LNMP
            Linux Nginx MySQL PHP
        LNMT
            Linux Nginx MySQL Tomcat

4).Nginx安装

安装方法:
1)epel源
        版本低,功能少
2)官方源
        官方编译好的,封装成rpm包,并提供yum源,推荐
3)源代码
        自己下载源代码,使用configure/make/make install
        安装复杂,费时

1.配置Nginx官方yum仓库
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

2.安装nginx,必须安装官方源的nginx
[root@web01 ~]# nginx -v
nginx version: nginx/1.16.1

[root@web01 ~]# rpm -ql nginx
Nginx日志轮询,日志切割
    /etc/logrotate.d/nginx
Nginx主配置文件
    /etc/nginx
    /etc/nginx/nginx.conf
    /etc/nginx/conf.d
    /etc/nginx/conf.d/default.conf  
Cgi、Fastcgi、Uwcgi配置文件    
    /etc/nginx/fastcgi_params
    /etc/nginx/scgi_params
    /etc/nginx/uwsgi_params
Nginx编码转换映射文件
    /etc/nginx/win-utf
    /etc/nginx/koi-utf
    /etc/nginx/koi-win
http协议的Content-Type与扩展名    
    /etc/nginx/mime.types
配置心态守护进程管理器
    /usr/lib/systemd/system/nginx.service
        systemctl reload nginx
Nginx模块目录
    /etc/nginx/modules
    /usr/lib64/nginx
    /usr/lib64/nginx/modules
Nginx终端管理命令
    /usr/sbin/nginx
        nginx -s reload
    /usr/sbin/nginx-debug
Nginx的帮助手册
    /usr/share/doc/nginx-1.16.1
    /usr/share/man/man8/nginx.8.gz
Nginx默认站点目录
    /usr/share/nginx
    /usr/share/nginx/html
    /usr/share/nginx/html/50x.html
    /usr/share/nginx/html/index.html
Nginx的缓存目录    
    /var/cache/nginx
Nginx的日志目录
    /var/log/nginx
[root@web01 ~]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --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'

问题一:
如果企业之前的Nginx都是通过源码安装,这时需要我们部署新的Nginx服务器,怎么实现
解法:
通过nginx -v 获得版本
通过nginx -V 获得configure选项

./configure --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'

问题二:
选项那么多,必须要配吗,网上搜的博文中选项没那么长
解法:
选项是根据企业需求来定制,最好按官方的或者企业的模板来实现


5)Nginx配置文件
Nginx住配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个
区块以一对大括号{}来表示开始与结束。

1.CoreModule 核心模块 全局配置
2.EventModule 事件驱动模块
3.HttpCoreModule http内核模块 局部

需了解扩展项
CoreModule层下可以有Event、Http
HTTP模块层允许有多个Server层,server主要用于配置多个网站
Server层又允许多个Location,Location主要用于定义网站访问路径

CoreModule核心模块

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

events事件模块

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

http内核模块

//公共的配置定义在http{}
http { //http层开始
...
    //使用Server配置网站,每个Server{}代表一个网站(简称虚拟主机)
    server {
        listen        80;             //监听端口,默认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    
    }    
}
[root@web01 ~]# egrep -v '^$|^.*#' /etc/nginx/conf.d/default.conf 
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

实例:

Nginx配置网站
目的:当我们访问game.oldboy.com的时候,访问/oldboy_code/里面的页面代码

1.在/etc/nginx/conf.d新建配置文件,要求一定是.conf结尾,然后将默认配置关闭
cd /etc/nginx/conf.d/
mv default.conf default.off
[root@web01 conf.d]# cat /etc/nginx/conf.d/game.conf 
server {
    listen 80;
    server_name game.oldboy.com;
    location / {
        root /oldboy_code;
        index index.html;
    }
}

配置完之后,语法测试
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

2.获取网页代码
1)创建网页目录
mkdir -p /oldboy_code/
2)将源码通过xshell拖拽至该目录
cd /oldboy_code
3)解压
[root@web01 oldboy_code]#  unzip html.zip 

3.启动或重载nginx
启动nginx
systemctl start nginx

重载nginx
方法一:
nginx -s reload
方法二:
systemctl reload nginx

4.通过域名访问,要修改windows的本地hosts文件
C:WindowsSystem32driversetchosts
10.0.1.7 game.oldboy.com

排错
1、解析问题
2、nginx无法启动
1)是否端口被占用(httpd)
2)配置文件写错了
3、重载失败
配置文件写错了
就算配置文件写错了,重载也不一定失败
一定要nginx -t 语法测试
4.一定看日志

原文地址:https://www.cnblogs.com/xmtxh/p/12266396.html