美文网首页
让nginx跑起来

让nginx跑起来

作者: PixelEyes | 来源:发表于2018-11-21 22:01 被阅读0次
    • 在linux ubuntu环境下的两种安装方案: yum / apt 安装、源码编译安装
    • 准备环境: linux服务器、gcc编译器、nginx源码
    • 获取nginx源码 : http://nginx.org
    • 编译安装nginx源码
    • 配置规则

    1.在你的web服务器上 下载nginx( nginx-1.14.1 pgp) (990kb)

    $ wget https://nginx.org/download/nginx-1.14.1.tar.gz
    

    2.下载完成后,解压文件

    $ tar zxvf nginx-1.14.1.tar.gz
    

    3.检查是否有gcc编译器,在控制台输入gcc

    pixel@ubuntu:~/Desktop/workspace/nginx-1.14.1$ gcc
    gcc: fatal error: no input files
    compilation terminated.
    

    如果安装了gcc会提示错误信息: fatal error: no input files
    否则会提示找不到gcc命令

    如果没有安装gcc,先安装

    $ sudo apt-get update
    $ sudo apt-get install gcc
    

    在nginx目录下执行 ./configure 检查当前编译环境

    nginx待编译目录结构如下:
    pixel@ubuntu:~/Desktop/workspace/nginx-1.14.1$ ll
    总用量 756
    drwxr-xr-x 8 pixel pixel   4096 11月  6 05:52 ./
    drwxrwxr-x 4 pixel pixel   4096 11月 21 02:52 ../
    drwxr-xr-x 6 pixel pixel   4096 11月  6 05:52 auto/
    -rw-r--r-- 1 pixel pixel 287441 11月  6 05:52 CHANGES
    -rw-r--r-- 1 pixel pixel 438114 11月  6 05:52 CHANGES.ru
    drwxr-xr-x 2 pixel pixel   4096 11月  6 05:52 conf/
    -rwxr-xr-x 1 pixel pixel   2502 11月  6 05:52 configure
    drwxr-xr-x 4 pixel pixel   4096 11月  6 05:52 contrib/
    drwxr-xr-x 2 pixel pixel   4096 11月  6 05:52 html/
    -rw-r--r-- 1 pixel pixel   1397 11月  6 05:52 LICENSE
    drwxr-xr-x 2 pixel pixel   4096 11月  6 05:52 man/
    -rw-r--r-- 1 pixel pixel     49 11月  6 05:52 README
    drwxr-xr-x 9 pixel pixel   4096 11月  6 05:52 src/
    pixel@ubuntu:~/Desktop/workspace/nginx-1.14.1$ gcc
    gcc: fatal error: no input files
    compilation terminated.
    
    通过 ./configure 开始检查编译环境
    
    pixel@ubuntu:~/Desktop/workspace/nginx-1.14.1$ ./configure 
    checking for OS
     + Linux 4.15.0-39-generic x86_64
    checking for C compiler ... found
     + using GNU C compiler
     + gcc version: 7.3.0 (Ubuntu 7.3.0-27ubuntu1~18.04) 
    
    

    检查完编译环境,如果有缺失的库会在下面提示出来.
    比如下面提示: 缺少PCRE库

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

    也可能缺少其他库:比如 zlib

    ./configure: error: the HTTP gzip module requires the zlib library.
    You can either disable the module by using --without-http_gzip_module
    option, or install the zlib library into the system, or build the zlib library
    statically from the source with nginx by using --with-zlib=<path> option.
    

    如果此时执行下一步编译 , 会提示 : make: *** 没有规则可制作目标“build”,由“default” 需求。 停止

    pixel@ubuntu:~/Desktop/workspace/nginx-1.14.1$ make
    make: *** 没有规则可制作目标“build”,由“default” 需求。 停止。
    

    那么我们需要先把缺少的 PCRE 库和 zlib 库安装完,如果已经安装,请忽略这一步

    $ sudo apt-get update 
    $ sudo apt-get install libpcre3 libpcre3-dev 
    $ sudo apt-get install zlib1g-dev
    
    • PCRE库支持正则表达式。如果我们在配置文件nginx.conf中使用了正则表达式,那么在编译Nginx时就必须把PCRE库编译进Nginx,因为Nginx的HTTP模块需要靠它来解析正则表达式。另外,pcre-devel是使用PCRE做二次开发时所需要的开发库,包括头文件等,这也是编译Nginx所必须使用的.
    • zlib是一个压缩库,类似于zip。你在linux下看到的*.gz文件,就是用zlib压缩的

    执行完./configure脚本之后,默认会在当前目录生成文件 Makefile
    产生Makefile文件后,直接输入make命令进行编译,make会在当前目录查找Makefile文件.
    找到后进行编译,下一步执行make install进行安装

    1.$ make
    2.$ make install
    

    通过编译安装后,会在 /usr/local 下生成一个nginx目录

    如通过yum安装,会在 /etc/ 下生成nginx目录
    下面以编译方法举例:

    • /usr/local/nginx/sbin,找到nginx,此文件是可执行文件,用于启动nginx
    $ cd /usr/local/nginx/sbin   
    $ ./nginx          >>>>>>>>>>>>>启动文件
    
    • 启动nginx前还需要修改 nginx.conf 配置文件
      nginx的配置文件在 /usr/local/nginx/conf目录,进入目录找到并修改文件: nginx.conf
    $ cd /usr/local/nginx/conf
    $ cat nginx.conf   >>>>>>>>>>>>>编辑配置文件
    

    nginx文件结构

    ... #全局块
    events {         #events块
        ...
    }
    http #http块
    {
        ... #http全局块
        server #server块
        {
            ... #server全局块
            location [PATTERN] #location块
            {
                ...
            }
            location [PATTERN]
            {
                ...
            }
        }
        server
        {
            ...
        }
        ... #http全局块
    }
    
    下面简单描述各个参数意义
    #user nobody; --以什么用户身份启动nginx (手动编译这个参数默认是屏蔽状态,用yum安装这个是生效状态)
    worker_processes 1;  --工作进程数(启动nginx时,会启动1个主管理进程,1个工作进程,工作进程默认数是1.当通过http访问nginx时,是工作进程在干活)
    #error_log logs/error.log;  --日志以及输出地址
    #pid logs/nginx.pid --进程管理 不用管
    events {
        worker_connections 1024; --工作进程的最大连接数 不是专业的服务器运维这个不用管
    }
    
    http{        #配置http协议
        include mime.types; #引用其他配置文件
        default_type application/octet-stream; #默认以流的形式传输数据
        sendfile ##可以直接下载文件
        keepalive_timeout #连接超时时间
        gzin #是否启用压缩
    
          //配置反向代理需要添加upstream web_crm{}块
          //配置多个反向代理,以IP和端口区分
          upstream web_crm{
                server 127.0.0.1:8080;
                server 127.0.0.2:8080;
                server 127.0.0.3:8080;
                #//可以配置多个server做负载,智能管理,自动切换
          }
         upstream web_XXX{
                server 127.0.0.1:8090;
          }
        server{    --虚拟站点 可以有多个server块
                 listen  --端口
                 server_name  localhost 网址
                 location / { --配置地址
                      root  html/monitor/;--配置根目录
                      index index.html index.htm   --配置首页
                 }
                 location /test/ { --配置子地址
                      root  html;--配置根目录
                      index text.html   --配置首页
                 }
                location /crm/ {
                        proxy_pass http://web_crm/crm/;  
                        #--代理路径,格式: 协议://上面配置的upstream名字/所代理地址的子目录
    
                        proxy_redirect off;
                        proxy_set_header Host $host;
                        #/////////////////////////////////////////////////////////////////////////////////////
                        //这部分内容复制粘贴就好,作用是配置转发的信息头
                        proxy_http_version 1.1;
                        proxy_set_header Upgrade $http_upgrade; #代理webscoket流量
                        proxy_cache_bypass $http_upgrade;#代理webscoket流量
    
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                        #/////////////////////////////////////////////////////////////////////////////////////
                        #//热备配置(后期总结)
                        #//变量配置(后期总结)
                        # 详细参见 nginx配置学习资料.pdf
                        #/////////////////////////////////////////////////////////////////////////////////////
                }
        }
    } 
    

    相关文章

      网友评论

          本文标题:让nginx跑起来

          本文链接:https://www.haomeiwen.com/subject/fcocqqtx.html