nginx安装(yum),Nginx安装(编译),Nginx配置文件

Nginx安装(yum)

yum安装Nginx,需要使用到epel-release源。这个可以先通过yum安装源。不过这个源安装的Nginx可能版本比较老。

你也可以自己弄一个Nginx源

写一个文件:

[root@shuai-01 ~]# vim /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo 
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ 
gpgcheck=0 
enabled=1

这时yum list 查看,Nginx源生效:

[root@shuai-01 ~]# yum list |grep nginx
collectd-nginx.x86_64                     5.8.0-4.el7                  epel     
munin-nginx.noarch                        2.0.40-4.el7                 epel     
nextcloud-nginx.noarch                    10.0.4-2.el7                 epel     
nginx.x86_64                              1:1.14.0-1.el7_4.ngx         nginx    
nginx-all-modules.noarch                  1:1.12.2-2.el7               epel     
nginx-debug.x86_64                        1:1.8.0-1.el7.ngx            nginx    
nginx-debuginfo.x86_64                    1:1.14.0-1.el7_4.ngx         nginx    
nginx-filesystem.noarch                   1:1.12.2-2.el7               epel     
nginx-mod-http-geoip.x86_64               1:1.12.2-2.el7               epel     
nginx-mod-http-image-filter.x86_64        1:1.12.2-2.el7               epel     
nginx-mod-http-perl.x86_64                1:1.12.2-2.el7               epel     
nginx-mod-http-xslt-filter.x86_64         1:1.12.2-2.el7               epel     
nginx-mod-mail.x86_64                     1:1.12.2-2.el7               epel     
nginx-mod-stream.x86_64                   1:1.12.2-2.el7               epel     
nginx-module-geoip.x86_64                 1:1.14.0-1.el7_4.ngx         nginx    
nginx-module-geoip-debuginfo.x86_64       1:1.14.0-1.el7_4.ngx         nginx    
nginx-module-image-filter.x86_64          1:1.14.0-1.el7_4.ngx         nginx    
nginx-module-image-filter-debuginfo.x86_64
                                          1:1.14.0-1.el7_4.ngx         nginx    
nginx-module-njs.x86_64                   1:1.14.0.0.2.4-1.el7_4.ngx   nginx    
nginx-module-njs-debuginfo.x86_64         1:1.14.0.0.2.4-1.el7_4.ngx   nginx    
nginx-module-perl.x86_64                  1:1.14.0-1.el7_4.ngx         nginx    
nginx-module-perl-debuginfo.x86_64        1:1.14.0-1.el7_4.ngx         nginx    
nginx-module-xslt.x86_64                  1:1.14.0-1.el7_4.ngx         nginx    
nginx-module-xslt-debuginfo.x86_64        1:1.14.0-1.el7_4.ngx         nginx    
nginx-nr-agent.noarch                     2.0.0-12.el7.ngx             nginx    
owncloud-nginx.noarch                     9.1.5-1.el7                  epel     
pcp-pmda-nginx.x86_64                     3.12.2-5.el7                 base     
python2-certbot-nginx.noarch              0.27.1-1.el7                 epel     

yum 安装:

[root@shuai-01 ~]# yum install -y nginx

启动Nginx:

[root@shuai-01 ~]# systemctl start nginx
[root@shuai-01 ~]# ps aux |grep nginx
root       5606  0.0  0.0  46368   968 ?        Ss   13:49   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      5607  0.0  0.1  46768  1932 ?        S    13:49   0:00 nginx: worker process
root       5609  0.0  0.0 112680   976 pts/1    R+   13:49   0:00 grep --color=auto nginx

关闭防火墙,selinux,就可以浏览器访问了。

查看版本和编译参数:

[root@shuai-01 ~]# nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

Nginx安装(源码)

先把原来yum 安装的remove掉

[root@shuai-01 nginx-1.14.0]# yum remove nginx

下载源码包:

[root@shuai-01 ~]# cd /usr/local/src/
[root@shuai-01 src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz

解压包:

[root@shuai-01 src]# tar zxvf nginx-1.14.0.tar.gz

编译安装:

[root@shuai-01 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx

[root@shuai-01 nginx-1.14.0]# make

[root@shuai-01 nginx-1.14.0]# make install

启动:

[root@shuai-01 nginx-1.14.0]# /usr/local/nginx/sbin/nginx 
[root@shuai-01 nginx-1.14.0]# ps aux |grep nginx
root       8392  0.0  0.0  20504   608 ?        Ss   14:15   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody     8393  0.0  0.1  20948  1340 ?        S    14:15   0:00 nginx: worker process
root       8395  0.0  0.0 112680   976 pts/1    R+   14:15   0:00 grep --color=auto nginx

这个时候想要停止Nginx,只能killall Nginx

可以通过启动脚本来管理:

[root@shuai-01 nginx-1.14.0]# vi /etc/init.d/nginx

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() 
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}

stop() 
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}

reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

restart()
{
    stop
    start
}

configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

exit $RETVAL

脚本启动:

[root@shuai-01 nginx-1.14.0]# /etc/init.d/nginx start
Reloading systemd:                                         [  确定  ]
Starting nginx (via systemctl):                            [  确定  ]
[root@shuai-01 nginx-1.14.0]# !ps
ps aux |grep nginx
root       8491  0.0  0.0  20504   604 ?        Ss   14:25   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     8492  0.0  0.1  20944  1336 ?        S    14:25   0:00 nginx: worker process
root       8494  0.0  0.0 112680   976 pts/1    R+   14:25   0:00 grep --color=auto nginx

设置开机自启动:

[root@shuai-01 nginx-1.14.0]# chkconfig --add nginx
[root@shuai-01 nginx-1.14.0]# chkconfig nginx on
[root@shuai-01 nginx-1.14.0]# chkconfig --list

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld         	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:关	4:开	5:开	6:关
nginx          	0:关	1:关	2:开	3:开	4:开	5:开	6:关

Nginx的配置文件

编译安装的Nginx的配置文件在: /usr/local/nginx/conf

[root@shuai-01 nginx-1.14.0]# cd /usr/local/nginx/conf
[root@shuai-01 conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf

配置文件结构:
全局配置(user、worker_processes、error_log、pid)
events(网络连接相关,worker_connections)
http(最重要的部分,大部分功能都放这里)
server(虚拟主机相关)
location(server里面)

全局部分:

user  nobody;//Nginx以那一个用户的身份来运行work进程
worker_processes  1;//用来定义work进程有几个,一般的这个数字和你的CPU核数来决定的,一个CPU核数跑一个work

error_log  logs/error.log crit ;//前面日志存在哪 ,后面的用来定义日志记录的级别。crit是记录级别比较高的,只会记录一些重要的
pid        logs/nginx.pid;//pid的位置

https://coding.net/u/aminglinux/p/nginx/git/blob/master/3z/global.md
events部分(和网络相关的):

worker_connections  1024;//每一个work允许最大的连接,最大数是65535
accept_mutex on;//当某一个时刻只有一个网络连接请求服务器时,服务器上有多个睡眠的进程会被同时叫醒,这样会损耗一定的服务器性能。Nginx中的accept_mutex设置为on,将会对多个Nginx进程(worker processer)接收连接时进行序列化,防止多个进程争抢资源。默认就是on。
multi_accept on;//nginx worker processer可以做到同时接收多个新到达的网络连接,前提是把该参数设置为on。默认为off,即每个worker process一次只能接收一个新到达的网络连接。

use epoll;
Nginx服务器提供了多个事件驱动器模型来处理网络消息。
其支持的类型有:select、poll、kqueue、epoll、rtsing、/dev/poll以及eventport。
* select:只能在Windows下使用,这个事件模型不建议在高负载的系统使用
* poll:Nginx默认首选,但不是在所有系统下都可用
* kqueue:这种方式在FreeBSD 4.1+, OpenBSD2.9+, NetBSD 2.0, 和 MacOS X系统中是最高效的	
* epoll: 这种方式是在Linux 2.6+内核中最高效的方式
* rtsig:实时信号,可用在Linux 2.2.19的内核中,但不适用在高流量的系统中
* /dev/poll: Solaris 7 11/99+,HP/UX 11.22+, IRIX 6.5.15+, and Tru64 UNIX 5.1A+操作系统最高效的方式	
* eventport: Solaris 10最高效的方式

http配置项:

官方文档 http://nginx.org/en/docs/

参考链接: https://segmentfault.com/a/1190000012672431

参考链接: https://segmentfault.com/a/1190000002797601

参考链接:http的header https://kb.cnblogs.com/page/92320/

MIME-Type

include mime.types; //cat conf/mime.types
定义nginx能识别的网络资源媒体类型(如,文本、html、js、css、流媒体等)

default_type  application/octet-stream;

定义默认的type,如果不定义改行,默认为text/plain.

log_format //访问日志格式

log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

其中main为日志格式的名字,后面的为nginx的内部变量组成的一串字符串。

access_log logs/access.log main;

定义日志的路径以及采用的日志格式,该参数可以在server配置块中定义。

sendfile on;

是否调用sendfile函数传输文件,默认为off,使用sendfile函数传输,可以减少user mode和kernel mode的切换,从而提升服务器性能。
对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。

sendfile_max_chunk 128k;

该参数限定Nginx worker process每次调用sendfile()函数传输数据的最大值,默认值为0,如果设置为0则无限制。

tcp_nopush on;

当tcp_nopush设置为on时,会调用tcp_cork方法进行数据传输。
使用该方法会产生这样的效果:当应用程序产生数据时,内核不会立马封装包,而是当数据量积累到一定量时才会封装,然后传输。这样有助于解决网络堵塞问题。
默认值为on。举例:快递员收快递、发快递,包裹累积到一定量才会发,节省运输成本。

keepalive_timeout 65 60;

该参数有两个值,第一个值设置nginx服务器与客户端会话结束后仍旧保持连接的最长时间,单位是秒,默认为75s。
第二个值可以省略,它是针对客户端的浏览器来设置的,可以通过curl -I看到header信息中有一项Keep-Alive: timeout=60,如果不设置就没有这一项。
第二个数值设置后,浏览器就会根据这个数值决定何时主动关闭连接,Nginx服务器就不操心了。但有的浏览器并不认可该参数。

send_timeout

这个超时时间是发送响应的超时时间,即Nginx服务器向客户端发送了数据包,但客户端一直没有去接收这个数据包。
如果某个连接超过send_timeout定义的超时时间,那么Nginx将会关闭这个连接。

client_max_body_size 10m;

浏览器在发送含有较大HTTP包体的请求时,其头部会有一个Content-Length字段,client_max_body_size是用来限制Content-Length所示值的大小的。
这个限制包体的配置不用等Nginx接收完所有的HTTP包体,就可以告诉用户请求过大不被接受。会返回413状态码。
例如,用户试图上传一个1GB的文件,Nginx在收完包头后,发现Content-Length超过client_max_body_size定义的值,
就直接发送413(Request Entity Too Large)响应给客户端。

gzip on;

是否开启gzip压缩。

gzip_min_length 1k;

设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是20。建议设置成大于1k的字节数,小于1k可能会越压越大。

gzip_buffers 4 16k;

设置系统获取几个单位的buffer用于存储gzip的压缩结果数据流。4 16k代表分配4个16k的buffer。

gzip_http_version 1.1;

用于识别 http 协议的版本,早期的浏览器不支持 Gzip 压缩,用户会看到乱码,所以为了支持前期版本加上了这个选项。
如果你用了Nginx反向代理并期望也启用Gzip压缩的话,由于末端通信是http/1.1,故请设置为 1.1。

gzip_comp_level 6;

gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)

gzip_types mime-type ... ;

匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的。
在conf/mime.conf里查看对应的type。

示例:gzip_types text/plain application/x-javascript text/css text/html application/xml;

gzip_proxied any;

Nginx作为反向代理的时候启用,决定开启或者关闭后端服务器返回的结果是否压缩,匹配的前提是后端服务器必须要返回包含”Via”的 header头。
以下为可用的值:
off - 关闭所有的代理结果数据的压缩
expired - 启用压缩,如果header头中包含 “Expires” 头信息
no-cache - 启用压缩,如果header头中包含 “Cache-Control:no-cache” 头信息
no-store - 启用压缩,如果header头中包含 “Cache-Control:no-store” 头信息
private - 启用压缩,如果header头中包含 “Cache-Control:private” 头信息
no_last_modified - 启用压缩,如果header头中不包含 “Last-Modified” 头信息
no_etag - 启用压缩 ,如果header头中不包含 “ETag” 头信息
auth - 启用压缩 , 如果header头中包含 “Authorization” 头信息
any - 无条件启用压缩
Copy

gzip_vary on;

和http头有关系,会在响应头加个 Vary: Accept-Encoding ,可以让前端的缓存服务器缓存经过gzip压缩的页面,例如,用Squid缓存经过Nginx压缩的数据。

server配置项:

nginx.conf server部分配置
server{} 包含在http{}内部,每一个server{}都是一个虚拟主机(站点)。

以下为nginx.conf配置文件中server{}部分的内容。

    server {
    listen       80;  //监听端口为80,可以自定义其他端口,也可以加上IP地址,如,listen 127.0.0.1:8080;
    server_name  localhost; //定义网站域名,可以写多个,用空格分隔。
    #charset koi8-r; //定义网站的字符集,一般不设置,而是在网页代码中设置。
    #access_log  logs/host.access.log  main; //定义访问日志,可以针对每一个server(即每一个站点)设置它们自己的访问日志。

    ##在server{}里有很多location配置段
    location / {
        root   html;  //定义网站根目录,目录可以是相对路径也可以是绝对路径。
        index  index.html index.htm; //定义站点的默认页。
    }

    #error_page  404              /404.html;  //定义404页面

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;  //当状态码为500、502、503、504时,则访问50x.html
    location = /50x.html {
        root   html;  //定义50x.html所在路径
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #定义访问php脚本时,将会执行本location{}部分指令
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;  //proxy_pass后面指定要访问的url链接,用proxy_pass实现代理。
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;  //定义FastCGI服务器监听端口与地址,支持两种形式,1 IP:Port, 2 unix:/path/to/sockt
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  //定义SCRIPT_FILENAME变量,后面的路径/scripts为上面的root指定的目录
    #    include        fastcgi_params; //引用prefix/conf/fastcgi_params文件,该文件定义了fastcgi相关的变量
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    # 
    #location ~ /\.ht {   //访问的url中,以/.ht开头的,如,www.example.com/.htaccess,会被拒绝,返回403状态码。
    #    deny  all;  //这里的all指的是所有的请求。
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;  //监听8000端口
#    listen       somename:8080;  //指定ip:port
#    server_name  somename  alias  another.alias;  //指定多个server_name

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443 ssl;  //监听443端口,即ssl
#    server_name  localhost;

### 以下为ssl相关配置
#    ssl_certificate      cert.pem;    //指定pem文件路径
#    ssl_certificate_key  cert.key;  //指定key文件路径

#    ssl_session_cache    shared:SSL:1m;  //指定session cache大小
#    ssl_session_timeout  5m;  //指定session超时时间
#    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   //指定ssl协议
#    ssl_ciphers  HIGH:!aNULL:!MD5;  //指定ssl算法
#    ssl_prefer_server_ciphers  on;  //优先采取服务器算法
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

Nginx架构分析

Nginx模块化
Nginx基于模块化设计,每个模块是一个功能实现,分布式开发,团队协作
核心模块、标准HTTP模块、可选HTTP模块、邮件模块、第三方模块
编译后的源码目录objs/ngx_modules.c
https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/module.md

Nginx的web请求机制
并行处理:多进程、多线程、异步
https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/IO.md
nginxweb请求机制:异步非阻塞

Nginx事件驱动模型
https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/event.md

设计架构概览
Nginx基于模块化设计、基于事件驱动模型处理请求、主进程和工作进程
https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/jg.md

猜你喜欢

转载自blog.csdn.net/aoli_shuai/article/details/83034970
今日推荐