nginx 编译安装、配置、使用

一、说明

  操作系统:centos 7.6(全新机器)

  安装nginx版本:nginx 1.15.2

 

二、准备工作

2.1、安装一系列需要用到的工具

yum groupinstall "Development Tools" -y
yum install expat-devel openssl-devel pcre-devel -y

    

2.2、下载所需要的依赖:apr、apr-util、pcre、zlib、openssl

  apr的下载地址:http://mirrors.hust.edu.cn/apache/apr/,选择最新的版本下载即可

cd /usr/local/src
wget http://mirrors.hust.edu.cn/apache/apr/apr-1.6.5.tar.gz
tar -zxf apr-1.6.5.tar.gz
cd apr-1.6.5
./configure --prefix=/usr/local/apr
make
make install

  

  apr-util的下载地址:http://mirrors.hust.edu.cn/apache/apr/,选择最新的版本下载即可

cd /usr/local/src
wget http://mirrors.hust.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
tar -zxf apr-util-1.6.1.tar.gz
cd apr-util-1.6.1
./configure --with-apr=/usr/local/apr --prefix=/usr/local/apr-util
make
make install

  

  pcre的下载地址:https://ftp.pcre.org/pub/pcre/,建议下载1.x版本,pcre2可能在编译nginx时出现问题

cd /usr/local/src
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
tar -zxf pcre-8.42.tar.gz
cd pcre-8.42
./configure --prefix=/usr/local/pcre
make
make install

  

  zlib的下载地址:https://www.zlib.net/,下载tar.gz压缩格式即可

cd /usr/local/src
wget https://www.zlib.net/zlib-1.2.11.tar.gz
tar -zxf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/usr/local/zlib
make
make install

  

  openssl的下载地址:https://www.openssl.org/source/,下载1.x版本,2.x版本可能在安装中出现确实头文件。

cd /usr/local/src
wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz
tar -zxf openssl-1.1.1d.tar.gz
cd openssl-1.1.1d
./config --prefix=/usr/local/openssl
make
make install

  

三、下载Nginx、安装

  在将工具和依赖的软件都安装后,就可以开始安装nginx了。

  nginx的下载地址:http://nginx.org/download/,我这里选择1.15.2版本

cd /usr/local/src
wget http://nginx.org/download/nginx-1.15.2.tar.gz
tar -zxf nginx-1.15.2.tar.gz
cd nginx-1.15.2

# 注意下面的--with-pcre、--with-zlib、--wite-openssl都是源码路径
./configure --prefix=/usr/local/nginx \
	--with-http_ssl_module \
	--with-pcre=/usr/local/src/pcre-8.42 \
	--with-zlib=/usr/local/src/zlib-1.2.11 \
	--with-openssl=/usr/local/src/openssl-1.1.1d

make
make install  

  注意,--with-pcre、--with-openssl、--with-zlib所指定的路径都是对应软件源码解压之后的目录,并不是安装目录。

  执行完毕后,如果没有出现错误,nginx就安装好了。

   

四、查看nginx文件目录

  前面在安装nginx时,我是将nginx的安装目录指定为/usr/local/nginx

[root@centos /usr/local/nginx]# ls
conf  html  logs  sbin

  conf目录存放nginx的相关配置文件;

  html目录存放网站的源码文件;

  logs存放日志文件;

  sbin包含nginx可执行脚本,用于启动nginx。

  

五、修改nginx配置

  nginx的配置文件为conf/nginx.conf。

  先创建www用户和www组,只创建www用户即可(会自动创建一个www组)

useradd www

  修改/usr/local/nginx/conf/nginx.conf文件,修改user为www,然后取消pid前面的注释即可。

user  www;
pid        logs/nginx.pid;

  

六、启动Nginx

  nginx的启动程序在nginx的安装目录下的sbin目录下

[root@centos /]# /usr/local/nginx/sbin/nginx

  检查端口信息:

[root@centos /]# lsof -i:80
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   10456 root    6u  IPv4  47721      0t0  TCP *:http (LISTEN)
nginx   10457  www    6u  IPv4  47721      0t0  TCP *:http (LISTEN)
nginx   10458  www    6u  IPv4  47721      0t0  TCP *:http (LISTEN)

  可以看到,nginx默认绑定到80端口。

  如果防火墙放行了80端口,那么访问机器ip,可以看到下面的页面:

   

 

七、Nginx的相关操作命令

[root@centos /]# nginx -h
nginx version: nginx/1.15.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file

  上面虽然有很多命令,一般使用最多的是-s选项:

  nginx -s stop   立即停止

  nginx -s quit    平滑停止进程

  nginx -s reopen  重新打开日志

  nginx -s reload   重新加载配置文件(热加载)

  注意,注意,启动nginx,只需要一个命令就是nginx,不需要加任何选项和任何参数。

  nginx -t  检测配置文件的语法是否有错误。

 

八、立即停止进程(stop)和平滑停止进程(quit)的区别

  可以用这种场景来理解,有一个请求,需要服务器端运行20秒才能返回给客户端结果。

  某一时刻,客户端发起了请求,隔了10秒,还没有获得响应结果(因为前面说了要20秒才能收到响应),所以还要等10秒。

  但是如果这个时候,

  1、使用nginx -s stop命令停止nginx服务器,那么,客户端会立即收到响应,这个响应不是用户希望收到的请求响应,而是服务器崩溃的响应。那么用户也就不用再等10秒来等待服务器的响应了,因为服务器已经关闭了。

  2、使用nginx -s quit命令停止nginx服务器,会发现当前的请求并不会立即终止,客户端可以看到服务器还在运行(注意这个时候,服务器已经quit了),10秒中之后(这个10秒加上之前等的10秒,刚好20秒),客户端收到了服务器的响应。这个响应不是服务器崩溃的响应,而是对之前请求进行的响应。但是,如果客户端此时再次发起一个请求,那么就会立即收到服务器错误的响应。

  所以,他们的区别是:是否立即终止当前正在执行的进程。立即停止(stop)就会立即停止,平滑停止(quit)不会立即停止正在进行的进程,而是等他执行完毕之后,再终止。

 

九、热加载

  首先,nginx的运行模式是 主master + 多worker 模式,master监听http请求,然后将请求交给空闲的worker去处理。

  1、没有新配置的时候,所有的worker都使用旧配置。

  2、当配置有更新的时候,如果某个worker还没有执行结束,那么他就继续执行,仍使用旧的配置。

  3、如果worker结束后,该worker不再继续服务,立即终止。

  4、新创建的worker使用新的配置文件。

  就这样,逐步将所有的旧worker都停止,创建新的worker使用新配置文件,达到热加载的目的。

 

10、配置文件

  具体如下(有注意点都写了)

user www www;
 
#设置工作进程数量(worker)
worker_processes auto;
 
#保存nginx进程id的文件路径
pid        /usr/local/nginx/logs/nginx.pid;
 
#设置一个进程最多可以打开多少个文件
worker_rlimit_nofile 51200;
 
events {
    #使用epoll模型
    use epoll;
 
    #每一个worker支持的连接数
    worker_connections 51200;
 
    multi_accept on;
}
 
#全局设置
http{
    include       mime.types;
    default_type  application/octet-stream;
 
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;
 
    sendfile   on;
    tcp_nopush on;
 
    keepalive_timeout 60;
 
    tcp_nodelay on;
 
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;
 
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
    gzip_vary on;
    gzip_proxied   expired no-cache no-store private auth;
    gzip_disable   "MSIE [1-6]\.";
 
    #limit_conn_zone $binary_remote_addr zone=perip:10m;
    ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.
 
    server_tokens off;
    
    #是否开启访问日志,如果开启的话,需要设置日志文件路径、日志格式
    #access_log off
    #默认的日志格式
    #log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
    #       '$status $body_bytes_sent "$http_referer" '
    #       '"$http_user_agent" $http_x
    #自定义日志格式
    log_format my_format '$remote_addr $request';
    
    #配置上游服务器
    upstream imageServer {
        server 192.168.1.2:80 weight=1 max_fails=2 fail_timeout=30s;
        server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=30s;
        server 192.168.1.4:80 weight=1 max_fails=2 fail_timeout=30s;
    }
 
    #配置虚拟主机,每一个server {  }都是配置一个虚拟主机,所以可以配置多个server{ }
    server {
        #监听的端口
        listen 80 default_server;
 
        #指定监听的域名
        server_name www.ganlixin.cn ganlixin.cn;
 
        #指定根路径
        root  /var/www;

        #指定默认首页
        index index.html index.htm index.php;

        #关闭访问日志,每一个虚拟主机的访问日志单独设置
        #access_log off
        #开启访问日志,使用默认的日志格式
        #access_log /var/www/nginx_access.log main;
        #开启访问日志,使用自定义的日志格式
        access_log /var/www/nginx_access.log my_format;

        #错误日志路径以及等级
        error_log  /var/www/ganlixin_error.log  crit;
         
        #错误页面
        #error_page   404   /404.html;
 
        #使用location来定义文件路径,相当于Apache的DocumentRoot,location后面跟~表示后面的是正则表达式匹配
        #配置php解析
        location ~ .*\.php(.*)$ {
            #解决无法获取PATH_INFO参数
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
        }
 
        include proxy-pass-php.conf;
 
        #对静态资源的请求单独处理
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
            #设置反向代理(静态资源请求另外一台机器),注意前缀http://
            proxy_pass          http://192.168.1.2:80;                
            proxy_set_header    X_Forwarded_For     $remote_addr;
        
            #设置资源过期时间
            expires      30d;
        }
 
        location ~ (.*)$ {
            #URL重写
            rewrite (.)$ /index.php$1;
        }
        location ~ /.well-known {
            allow all;
        }
 
        location ~ /\.{
            deny all;
        }
    }
 
    #单独配置虚拟机的配置文件
    include vhost/*.conf;
}

  

 

posted @ 2018-07-31 15:04  寻觅beyond  阅读(1023)  评论(0编辑  收藏  举报
返回顶部