• Nginx web基础入门


    Nginx web基础入门

    两种部署方式:

    1)yum安装

    更改官方源:

    [root@web ~]# vim /etc/yum.repos.d/nginx.repo
    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/7/$basearch/
    gpgcheck=0
    enabled=1
    

    安装依赖包

    yum install -y gcc gcc-c++ autoconf pcre pcre-devel openssl-devel make automake
    

    安装nginx-1.16.0

    yum install -y nginx
    

    启动nginx并设置开机自启

    [root@web01 ~]# systemctl start nginx
    [root@web01 ~]# systemctl enable nginx
    

    启动报错:

    1565683277023

    报错原因:80端口被占用

    解决方法:谁占用的,干掉谁

    systemctl stop httpd
    

    监测nginx是否启动成功

    #方法一:监测进程
    [root@web01 ~]# ps -ef|grep [n]ginx
    root      12457      1  0 11:44 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
    nginx     12458  12457  0 11:44 ?        00:00:00 nginx: worker process
    
    #方法二:监测端口
    [root@web01 ~]# netstat -lntup|grep 80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12457/nginx: master 
    
    #方法三:systemd
    [root@web01 ~]# systemctl status nginx
    ● nginx.service - nginx - high performance web server
       Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
       Active: active (running) since Tue 2019-08-13 11:44:03 CST; 8min ago
         Docs: http://nginx.org/en/docs/
      Process: 12456 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
     Main PID: 12457 (nginx)
       CGroup: /system.slice/nginx.service
               ├─12457 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
               └─12458 nginx: worker process
    
    #方法四:打开浏览器访问
    
    #方法五:curl命令
    [root@web01 ~]# curl 10.0.0.7
    [root@web01 ~]# curl www.driverzeng.com
    

    查看nginx的版本

    [root@web01 ~]# nginx -v
    nginx version: nginx/1.16.0
    

    2)源码安装

    解压

    tar xf nginx-1.16.0.tar.gz
    #创建www用户
    groupadd www -g 666
    useradd www -u 666 -g 666 -s /sbin/nologin -M
    

    生成

    ./configure --prefix=/usr/local/nginx-1.16.0 
    --user=www 
    --group=www 
    --with-http_ssl_module 
    --with-http_stub_status_module 
    --with-stream
    

    编译

    make
    

    安装

    make install
    

    如何升级nginx或者添加功能

    ln -s /usr/local/nginx-1.16.0 /usr/local/nginx
    
    #升级版本
    ./configure --prefix=/usr/local/nginx-1.17.2 
    --user=www 
    --group=www 
    --with-http_ssl_module 
    --with-http_stub_status_module 
    --with-stream 
    --with-http_mp4_module
    
    #重新软链接
    rm -f /usr/local/nginx && ln -s /usr/local/nginx-1.17.2 /usr/local/nginx
    -----------------------------------------------------------------------------------------
    #添加模块
    ./configure --prefix=/usr/local/nginx_new-1.16.0 
    --user=www 
    --group=www 
    --with-http_ssl_module 
    --with-http_stub_status_module 
    --with-stream 
    --with-http_mp4_module
    
    #重新软链接
    rm -f /usr/local/nginx && ln -s /usr/local/nginx_new-1.16.0 /usr/local/nginx
    

    Ansible,SaltStack

    先源码安装,然后打rpm包,放到yum仓库,然后yum安装

    使用systemd管理nginx

    vim /usr/lib/systemd/system/nginx.service
    [Unit]
    Description=nginx - high performance web server
    Documentation=http://nginx.org/en/docs/
    After=network-online.target remote-fs.target nss-lookup.target
    Wants=network-online.target
    
    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStart=/usr/local/nginx/sbin/nginx #命令路径 -c /usr/local/nginx/conf/nginx.conf #主配置文件路径
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s TERM $MAINPID
    
    [Install]
    WantedBy=multi-user.target
    

    nginx相关配置文件

    1.Nginx主配置文件形参

    路径 类型 作用
    /etc/nginx/nginx.conf 配置文件 nginx主配置文件
    /etc/nginx/conf.d/default.conf 配置文件 默认网站配置文件

    2.Nginx代理相关参数文件

    路径 类型 作用
    /etc/nginx/fastcgi_params 配置文件 Fastcgi代理配置文件
    /etc/nginx/scgi_params 配置文件 scgi代理配置文件
    /etc/nginx/uwsgi_params 配置文件 uwsgi代理配置文件

    3.Nginx编码相关配置文件

    路径 类型 作用
    /etc/nginx/win-utf 配置文件 Nginx编码转换映射文件
    /etc/nginx/koi-utf 配置文件 Nginx编码转换映射文件
    /etc/nginx/koi-win 配置文件 Nginx编码转换映射文件
    /etc/nginx/mime.types 配置文件 Content-Type与扩展名

    4.Nginx管理相关命令

    路径 类型 作用
    /usr/sbin/nginx 命令 Nginx命令行管理终端工具
    /usr/sbin/nginx-debug 命令 Nginx命令行与终端调试工具

    4.Nginx日志相关目录与文件

    路径 类型 作用
    /var/log/nginx 目录 Nginx默认存放日志目录
    /etc/logrotate.d/nginx 配置文件 Nginx默认的日志切割

    nginx的配置文件详解

    Nginx主配置文件整体分为三块进行学习,分别是CoreModule(核心模块)EventModule(事件驱动模块)HttpCoreModule(http内核模块)

    Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。
    
    Nginx主配置文件整体分为三块进行学习,分别是CoreModule(核心模块),EventModule(事件驱动模块),HttpCoreModule(http内核模块)
    
    CoreModule核心模块
    
    user www;                       #Nginx进程所使用的用户
    worker_processes 1;             #Nginx运行的work进程数量(建议与CPU数量一致或auto)
    error_log /log/nginx/error.log  #Nginx错误日志存放路径
    pid /var/run/nginx.pid          #Nginx服务运行后产生的pid进程号
    EventModule(事件驱动模块)
    
    events {            
        worker_connections 25535;   #每个worker进程支持的最大连接数
        use epoll;                  #事件驱动模型, epoll默认
    }
    HttpCoreModule(http内核模块)
    
    #http层开始
    http {
    #包含资源类型文件
        include       /etc/nginx/mime.types;
    #默认以下载方式传输给浏览器(前提是该资源在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;
    #搭配sendfile使用
        #tcp_nopush     on;
    #长连接超时时间
        keepalive_timeout  65;
    #是否开启压缩
        #gzip  on;
    
        #使用Server配置网站, 每个Server{}代表一个网站(简称虚拟主机)
        'server' {
            listen       80;            #监听端口, 默认80
            server_name  driverzeng.com;    #提供的域名
            access_log  access.log;     #该网站的访问日志
            #控制网站访问路径
            'location' / {
                root   /usr/share/nginx/html;   #存放网站源代码的位置
                index  index.html index.htm;    #默认返回网站的文件
            }
        }
        ...
        #第二个虚拟主机配置
        'server' {
        ...
        }
    
        include /etc/nginx/conf.d/*.conf;  #包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件
    } #http结束层
    

    日志格式

    log_format  abd  '$remote_addr - |$remote_user| [$time_local] "$request" '
                     '$status $body_bytes_sent $bytes_sent  "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';
    
    
    $remote_addr        # 记录客户端IP地址
    $remote_user        # 记录客户端用户名
    $time_local         # 记录通用的本地时间
    $time_iso8601       # 记录ISO8601标准格式下的本地时间
    $request            # 记录请求的方法以及请求的http协议
    $status             # 记录请求状态码(用于定位错误信息)
    $body_bytes_sent    # 发送给客户端的资源字节数,不包括响应头的大小
    $bytes_sent         # 发送给客户端的总字节数
    $msec               # 日志写入时间。单位为秒,精度是毫秒。
    $http_referer       # 记录从哪个页面链接访问过来的
    $http_user_agent    # 记录客户端浏览器相关信息
    $http_x_forwarded_for #记录客户端IP地址
    $request_length     # 请求的长度(包括请求行, 请求头和请求正文)。
    $request_time       # 请求花费的时间,单位为秒,精度毫秒
    # 注:如果Nginx位于负载均衡器,nginx反向代理之后, web服务器无法直接获取到客 户端真实的IP地址。
    # $remote_addr获取的是反向代理的IP地址。 反向代理服务器在转发请求的http头信息中,
    # 增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址。
    

    game日志记录实战

    location /favicon.ico{
    	access_log off;
    	return 200;
    }
    
    location /js/common.js{
    	access_log /var/log/nginx/js.log main;
    }
    

    日志切割

    cat /etc/logrotate.d/nginx
    /var/log/nginx/*.log {
            daily                   # 每天切割日志
            missingok               # 日志丢失忽略
            rotate 52               # 日志保留52天
            compress                # 日志文件压缩
            delaycompress           # 延迟压缩日志
            notifempty              # 不切割空文件
            create 640 nginx adm    # 日志文件权限
            sharedscripts
            postrotate      # 切割日志执行的命令
                    if [ -f /var/run/nginx.pid ]; then #判断是否有pid
                            kill -USR1 `cat /var/run/nginx.pid`#重新加载
                    fi
            endscript
    }
    

    手写虚拟主机

    1.编辑虚拟主机,创建查询目录

    [root@web01 conf.d]# vim game.conf
    server {
    #监听的端口
        listen 80;
    #域名,_;(要写ip) localhost;
        server_name localhost;
    #location 匹配规则
        location / {
    #指定站点目录
                root /code/h5_games;
    #指定索引页面(默认主页)
                index index.html;
        }
    
    }
    #创建站点目录
    [root@web01 conf.d]# mkdir /code
    #授权www用户给站点目录
    [root@web01 conf.d]# chown -R www.www /code
    #手动创建index
    [root@web01 code]# echo game > /code/index.html
    
    -----------------------------源码包编写-------------------------------------
    注意:
    1.nginx -V 找见nginx主配置文件,之后编写虚拟主机,单独创建目录;
    2.注释主配置文件,除了调用include /etc/nginx/game.conf#写绝对路径
    3.nginx -t#检查 
    4.重启
    5.其他基本一致
    

    2.上传,检查

    [root@web01 code]# ll /code/
    total 18860
    -rw-r--r-- 1 root root 19304923 Aug 14 11:36 h5_games.zip
    -rw-r--r-- 1 www  www         5 Aug 14 11:51 index.html
    [root@web01 code]# pwd
    /code
    [root@web01 code]# unzip h5_games.zip 
    

    3.检查nginx的语法是否存在错误

    [root@web01 code]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    

    4.重载Nginx [reload|restart]

    [root@web01 code]# systemctl reload nginx
    

    5.设置hosts(基于域名)

    MacBook-Pro:~ driverzeng$ sudo vim /etc/hosts#Windows下也可以
    10.0.0.101 game.driverzeng.com
    

    6.检测是否设置成功

    MacBook-Pro:~ driverzeng$ ping game.driverzeng.com
    PING game.driverzeng.com (10.0.0.101): 56 data bytes
    64 bytes from 10.0.0.101: icmp_seq=0 ttl=64 time=0.267 ms
    64 bytes from 10.0.0.101: icmp_seq=1 ttl=64 time=0.450 ms
    64 bytes from 10.0.0.101: icmp_seq=2 ttl=64 time=0.508 ms
    64 bytes from 10.0.0.101: icmp_seq=3 ttl=64 time=0.464 ms
    

    7.浏览器检测

    打开浏览器访问:http://game.driverzeng.com

    img

    虚拟主机

    通常在企业中可能会有很多业务系统,那么多套业务服务如何使用Nginx配置?

    img

    如果使用如上方式部署,则需要多台服务器配置Nginx,但如果使用虚拟主机方式,则在同一个Nginx上运行多套单独服务,这些服务是相互独立的。简单来说,看似多套业务系统,实则可以运行在一台Nginx服务上

    img

    Nginx配置虚拟主机有如下三种方式:

    方式一、基于主机多IP方式

    方式二、基于端口的配置方式

    方式三、基于多个hosts名称方式(多域名方式)


    img

    • 那么基于多IP的方式,有如下两种方式:

    img

    1).配置多网卡多IP的方式

    server {
        ...
        listen 10.0.0.10:80;
        ...
    }
     
    server {
        ...
        listen 10.0.0.11:80;
        ...
    }
    

    2).配置单网卡多IP的方式

    #添加一个IP
    [root@web01 ~]# ip addr add 10.0.0.11/24 dev eth0
     
    # 虚拟机配置方案
    [root@web01 ~]# cat /etc/nginx/conf.d/addr1.conf
    server {
        ...
        listen 10.0.0.10:80;
        ...
    }
     
    [root@web01 ~]# cat /etc/nginx/conf.d/addr2.conf
    server {
        ...
        listen 10.0.0.11:80;
        ...
    }
    

    img

    Nginx多端口虚拟主机方式,具体配置如下

    #仅修改listen监听端口即可, 但不能和系统端口出现冲突
     
    [root@web01 ~]# cat /etc/nginx/conf.d/port1.conf
    server {
        ...
        listen 80;
        ...
    }
     
    [root@web01 ~]# cat /etc/nginx/conf.d/port2.conf
    server {
        ...
        listen 81;
        ...
    }
     
    [root@web01 ~]# cat /etc/nginx/conf.d/port3.conf
    server {
        ...
        listen 82;
        ...
    }
    

    img

    1.创建对应的**web站点目录以及程序代码

    [root@web01 ~]# mkdir /soft/code/{server1,server2}
    [root@web01 ~]# echo "server1" > /code/server1/index.html
    [root@web01 ~]# echo "server2" > /code/server2/index.html
    

    2.配置不同域名的虚拟主机

    [root@web02 ~]# cat /etc/nginx/conf.d/server1.conf
    server {
        listen       80;
        server_name  1.oldboyedu.com;
        root /code/server1;
        index index.html;
        ...
    }
    [root@web01 ~]# cat /etc/nginx/conf.d/server2.conf
    server {
        ...
        listen       80;
        server_name  2.oldboyedu.com;
        root /code/server2;
        index index.html;
    }
    
  • 相关阅读:
    解决-webkit-box-orient: vertical;(文本溢出)属性在webpack打包后无法编译的问题
    消息框尖尖
    表单提交
    昨天看了一个大神的fix类,清晰了然
    使用cross-env解决跨平台设置NODE_ENV的问题
    axios 在Vue全局引入的方法
    vue自定义指令
    AMD/CMD/CommonJs到底是什么?它们有什么区别?
    artDialog.js的使用
    delegate-使用笔记
  • 原文地址:https://www.cnblogs.com/1naonao/p/11358063.html
Copyright © 2020-2023  润新知