Centos7.2 安装nignx

1、准备安装环境

rpm -qa|grep gcc
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

2、下载编译包

https://nginx.org/en/download.html
我用的是nginx-1.14.2.tar.gz

# 解压
tar -xvf nginx-1.14.2.tar.gz
# 进入目录
cd nginx-1.14.2

交叉编译

./configure --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module

make

make && make install

make 完成后默认会在/usr/local下生成一个nginx目录

3、配置nginx.conf

Nginx 编译参数

  • --prefix=path —— 定义Nguni 服务所在的文件夹,服务根路径,用于设置其他由configure设置的相对路径(除了用到的源库路径)。默认是/usr/local/nginx
  • --sbin-path=path —— 设置nginx可执行文件的名字,默认是prefix/sbin/nginx
  • --conf-path=path —— 设置nginx配置文件的名字。nginx可以通过指定使用不同的配置文件来启动,加命令参数为-c file。默认nginx配置文件名为prefix/conf/nginx.conf。
  • --pid-path=path —— 设置保存nginx主进程process ID的文件名。安装完毕后也可以在nginx.conf中修改,使用pid选项。该文件默认命名为prefix/logs/nginx.pid。
  • --error-log-path=path —— 设置关键错误、警告和诊断的日志文件名。安装完毕后也可以在nginx.conf中修改,使用error_log选项。该文件默认命名为prefix/logs/error.log。
  • --http-log-path=path —— 设置http服务请求的日志文件名。安装完毕后,可在nginx.conf中修改,使用access_log选项。默认文件名prefix/logs/access.log。
  • --build=name —— 设置一个可选的nginx别名
  • --user=name —— 设置工作进程的用户名(一般是非特权用户),安装完毕后,可在nginx.conf中通过选项user修改。默认为nobody。
  • --group=name —— 设置工作进程的用户组,安装完毕后,可在nginx.conf中通过选项user修改。默认情况下,用户组名称是非特权用户组名称。
  • --with-select_module
  • --without-select_module —— 启用或禁用一个允许服务使用select()方法的模块。如果平台没有更适合的方法,例如kqueue, epoll, or /dev/poll,这个模块会自动编译。
    我的理解是:--with-select_module表示将编译select_module模块
  • --without-select_module 表示不编译select_module模块
  • --with-poll_module
  • --without-poll_module—— 启用或禁用一个允许服务使用poll()方法的模块。如果平台没有更适合的方法,例如kqueue, epoll, or /dev/poll,这个模块会自动编译。
  • --without-http_gzip_module —— 禁止编译压缩http服务响应的模块,该模块构建和启用依赖zlib库。
  • --without-http_rewrite_module —— 禁止编译允许http服务重定向请求和更改请求URI的模块,该模块构建和启用依赖pcre库。
  • --without-http_proxy_module —— 禁止编译http服务代理模块。
  • --with-http_ssl_module—— 启用编译一个支持把https加到http服务的模块。该模块默认不编译,依赖OpenSSL库。
  • --with-pcre=path —— 设置pcre库源码路径。该库是正则表达式所必须的,推荐安装。
  • --with-pcre-jit —— 用即时编译构建pcre库。
  • --with-zlib=path —— 设置zlib库源码路径。该库是压缩模块所必须的,推荐安装。
  • --with-cc-opt=parameters —— 该选项设置的参数将被添加到CFLAGS变量,在FreeBSD系统中使用系统pcre库时,--with-cc-opt="-I /usr/local/include"将被指定。如果select()支持的文件数量需要增加,该参数可指定如下--with-cc-opt="-D FD_SETSIZE=2048"
  • --with-ld-opt=parameters —— 该选项设置的参数将在链接时使用。在FreeBSD系统中使用系统pcre库时,指定 --with-ld-opt="-L /usr/local/lib"

4、常用配置

# 启动nginx的用户名和组名
user  develope develope;
# 工作线程数量,此配置最好为cpu核心数量或者2倍
worker_processes  4;

events {
    # worker_connections 指定最大可以同时接收的连接数量,这里一定要注意,最大连接数量是和worker processes共同决定的。
    worker_connections  4096;
    #  配置指定nginx在收到一个新连接通知后尽可能多的接受更多的连接
    multi_accept        on;
    # 如果是linux2.6+,使用epoll,如果是BSD如Mac请使用Kqueue
    use                 epoll;
}
# 核心配置  配置虚拟主机集合
http {
    include       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"';

    # http请求包最大数据 30m
    client_max_body_size 30m;

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  65;
    types_hash_max_size 2048;

    # 设定请求缓冲
    client_header_buffer_size    128k; # 指定客户端请求头缓存大小,当请求头大于 1KB 时会用到该项
    large_client_header_buffers  4 128k; # 最大数量和最大客户端请求头的大小


    ##
    #    # SSL Settings
    #
    #
    ##配置共享会话缓存大小
    ssl_session_cache   shared:SSL:10m;
    # 配置会话超时时间
    ssl_session_timeout 10m;


    ##
    #    # Logging Settings
    #        ##
    #
    access_log     /usr/local/lib/logs/nginx/http-access.log;  ## 访问日志
    error_log      /usr/local/lib/logs/nginx/http-error.log;    ## 错误日志

    # 启用gzip
    gzip     on;
    gzip_disable "msie [1-6]";

    # 负载均衡配置
    upstream service.upstream.com {
        # 轮循模式
        # weight           权重
        # ip_hash         通过ip的hash值
        # backup          其它所有非backup都down的时候启用backup
        # fair                 按后端服务器的响应时间来分配请求,响应时间短的优先分配。与weight分配策略类似。
        # down              不参与
        server 172.26.203.175:8020 weight=10;                              #  ZZ-APP-SERVER-003
        server 172.26.203.174:8020 weight=10;                              #  ZZ-APP-SERVER-002
        server 172.26.203.172:8020 weight=10;                              #  ZZ-APP-SERVER-001
    }
      
     # 定义绑定主机
     server {
        # 监听端口
        listen       80;
        # 此主机监听host_name
        server_name  www.xxx.com;                         # API service
        # 日志
        access_log   /usr/local/lib/logs/nginx/host.service.log  main;
        
       # location 配置可以有多个    遵守最长匹配原则
       # 普通配置
        location / {
              root                    /usr/local/lib/www;
              index                  index.html    index.htm;
        }

        # 转发配置
        location /api/ {
            proxy_pass             http://service.upstream.com;
            proxy_set_header       Host $host;
            proxy_set_header       X-real-ip $remote_addr;
            proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        # 特殊处理
        if ($host = "www.xxx.com") {
           return 301 https://$server_name$request_uri;
        }
    }
  
    # https
    server {
        listen        443 ssl;
        server_name   www.xxx.com;

        #设置长连接
        keepalive_timeout   70;

       #  证书地址
        ssl_certificate       /usr/local/nginx/cert/www.xxx.com.pem;
        ssl_certificate_key   /usr/local/nginx/cert/www.xxx.com.key;
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         HIGH:!aNULL:!MD5;

        #优先采取服务器算法
        ssl_prefer_server_ciphers on;

        #  HSTS策略
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains;preload" always;

        #防XSS攻擊
        add_header X-Xss-Protection 1;

        location / {
            proxy_pass                 http://service.upstream.com;
            proxy_set_header       Host $host;
            proxy_set_header       X-real-ip $remote_addr;
            proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

    # 引入其它配置
    include /usr/local/lib/web-http.conf;
}

区别手机端
if ( $http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT-)|(SonyEricsson)|(NEC-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC-)|(SED-)|(EMOL-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" )
{
root /usr/local/website/mobile;
}

你可能感兴趣的:(Centos7.2 安装nignx)