Nginx安装&配置

2019-04-18  本文已影响0人  芝士就是力量007

所有操作均使用root权限

一、准备工作

安装nginx之前先安装以下工具,centos平台编译环境使用如下指令

安装make

> yum -y install gcc automake autoconf libtool make

安装g++

> yum install gcc gcc-c++

一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。

安装PCRE库

> cd /server/download
> wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz
或者
> wget https://netix.dl.sourceforge.net/project/pcre/pcre/8.40/pcre-8.40.tar.gz
> tar -zxvf pcre-8.42.tar.gz
> cd pcre-8.42
> ./configure
> make
> make install

安装zlib库

> cd /server/download
> wget http://zlib.net/zlib-1.2.11.tar.gz
> tar -zxvf zlib-1.2.11.tar.gz
> cd zlib-1.2.11
> ./configure
> make
> make install

安装ssl

> cd /server/download
> wget https://www.openssl.org/source/openssl-1.0.2t.tar.gz
> tar -zxvf openssl-1.0.2t.tar.gz

二、正式安装nginx

> cd /server/download
> wget http://nginx.org/download/nginx-1.12.0.tar.gz
> tar -zxvf nginx-1.12.0.tar.gz
> cd nginx-1.12.0

设置构建模块

> ./configure --prefix=/server/service/nginx \
--with-http_ssl_module \
--with-openssl=/server/download/openssl-1.0.2t \
--with-pcre=/server/download/pcre-8.42 \
--with-zlib=/server/download/zlib-1.2.11 \
--with-http_stub_status_module \
--with-threads
> make
> make install

三、nginx常用命令

> ./sbin/nginx -t -c ./conf/nginx.conf
> ./sbin/nginx -c ./conf/nginx.conf
> ./sbin/nginx -s reload
> ./sbin/nginx -s reopen
> ./sbin/nginx -v

四、nginx配置说明

#user  nobody;
worker_processes  1;#允许生成的进程数,默认为1

#制定日志路径,级别。这个设置可以放入全局块,http块,server块,
#级别以此为:debug|info|notice|warn|error|crit|alert|emerg
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;#指定nginx进程运行文件存放地址

events {
    #设置网路连接序列化,防止惊群现象发生,默认为on
    accept_mutex on; 
    #设置一个进程是否同时接受多个网络连接,默认为off
    #事件驱动模型select|poll|kqueue|epoll|resig|/dev/poll|eventport   
    multi_accept on; 
    #use epoll;
    worker_connections  1024;    #最大连接数,默认为512
}

http {
    #设置是否开启对后端响应的gzip压缩,然后返回压缩内容给前端
    gzip on;
    #设定进行gzip压缩的阈值
    #当后端response的Content-Length大小小于该值则不进行gzip压缩
    gzip_min_length 1k;
    #设置用于压缩后端response的buffer的数量和每个的大小,
    #默认每个buffer大小为一个内存页
    #根据平台不同可能是4k或8k。这个限制了nginx不能压缩大于64k的文件
    gzip_buffers 4 16k;
    #指定gzip压缩的级别,默认为1,该值可设置的范围是1-9,1为最小化压缩(处理速度快),
    #9为最大化压缩(处理速度慢),数字越大压缩的越好,也越占用CPU时间
    gzip_comp_level 2;
    #指定哪些mime types启用gzip压缩,默认text/html
    gzip_types text/plain application/javascript text/css;
    gzip_vary off;#是否往response header里头写入Vary: Accept-Encoding
    gzip_disable "MSIE [1-6]\.";#正则匹配User-Agent中的值,匹配上则不进行gzip

    include       mime.types;#文件扩展名与文件类型映射表
    default_type  application/octet-stream;#默认文件类型,默认为text/plain

    #自定义格式
    #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  logs/access.log  main;#main为日志格式的默认值
    #access_log off; #取消服务日志
    #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    sendfile        on;
    #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
    sendfile_max_chunk 100k;  
    #tcp_nopush     on;
    #连接超时时间,默认为75s,可以在http,server,location块。
    keepalive_timeout  65;
    #nginx服务器与被代理的服务器建立连接的超时时间,默认60秒
    proxy_connect_timeout       60;
    #nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。
    proxy_send_timeout          60;
    #nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。
    proxy_read_timeout          60;
    #指定客户端的响应超时时间。这个设置不会用于整个转发器,而是在两次客户端读取操作之间。
    #如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。
    send_timeout                60;
    #错误页
    error_page 404 https://www.baidu.com; 

    #文件服务器配置
    server {
        listen       80;
        listen       443;
        server_name  file.xxx.com;

        location / {
            root  /nfs/test;
            autoindex off;
        }

    }

    #上游服务反向代理配置
    server {
        listen       80;
        listen       443;
        server_name  www.xxx.com;
        client_max_body_size 100M;#上传文件最大大小

        #配置静态资源
        location ~ .*\.(html|js|css|ico|png|jpg|eot|svg|ttf|woff|gif) {
             root    /opt/page/web/dist/;
             allow   all;
        }
        location =/index.html {
             root    /opt/page/web/dist/;
             index   index.html ;
        }
       #配置上游后台服务
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass   http://127.0.0.1:8100;
        }
    }
}

五、nginx添加lua模块

1、安装LuaJIT

> cd /server/download
> wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
> tar xzvf LuaJIT-2.0.5.tar.gz
> cd LuaJIT-2.0.5
> make PREFIX=/usr/local/luajit
> make install PREFIX=/usr/local/luajit

2、下载ngx_devel_kit (NDK)

> cd /server/download
> wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz
> tar xzvf v0.3.0.tar.gz

3、下载ngx_lua

> cd /server/download
> wget https://github.com/openresty/lua-nginx-module/archive/v0.10.11.tar.gz
> tar xvf v0.10.11.tar.gz

4、查看之前编译参数

> ./sbin/nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2t  10 Sep 2019
TLS SNI support enabled
configure arguments: --prefix=/server/service/nginx --with-http_ssl_module 
--with-openssl=/server/download/openssl-1.0.2t 
--with-pcre=/server/download/pcre-8.42 
--with-zlib=/server/download/zlib-1.2.11 
--with-http_stub_status_module --with-threads

5、集成添加lua

> export LUAJIT_LIB=/usr/local/luajit/lib
> export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
> ./configure --prefix=/server/service/nginx \
--with-http_ssl_module \
--with-openssl=/server/download/openssl-1.0.2t \
--with-pcre=/server/download/pcre-8.42 \
--with-zlib=/server/download/zlib-1.2.11 \
--with-http_stub_status_module \
--with-threads \
--with-ld-opt="-Wl,-rpath,/usr/local/luajit/lib" \
--add-module=/server/download/ngx_devel_kit-0.3.0 \
--add-module=/server/download/lua-nginx-module-0.10.11

> make -j2
> make install

#停下nginx服务,将现有的nginx程序备份,将新生产的覆盖,启动
> cp ./sbin/nginx ./sbin/nginx.bak
> cp ./objs/nginx ./sbin/
#启动nginx
> ./sbin/nginx

6、验证lua
在nginx.conf配置中加入如下代码

location /hello_lua { 
   default_type 'text/plain'; 
   content_by_lua 'ngx.say("hello, lua")'; 
}

访问ip+/hello_lua会出现”hello, lua”表示安装成功

hello, lua

结束

参考资料

http://www.nginx.cn/install

上一篇 下一篇

猜你喜欢

热点阅读