我下面给大家带来的是NGINX的一些记录,因为可能要换工作了,面试的时候会问到,而且有个人老问我,他要给公司配置NGINX,我就记录下吧

    一、nginx的安装

安装前的一些准备工作

[root@taotao2016 ~]# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@taotao2016 ~]#

yum install pcre pcre-devel  -y  #先安装pcre 这是nginx rewrite模块要用到的必须要安装

yum install openssl-devel openssl #这也要安装  我用的7的系统直接yum安装的

useradd nginx -s /sbin/nologin -M  #建立ningx用户

开始安装

nginx-1.11.3.tar.gz   http://nginx.org/ 下载地址

 ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.11.3  --with-http_stub_status_module --without-http_ssi_module  #安装的目录加上版本号

make && make install

这样就安装完成了

[root@taotao2016 nginx-1.11.3]# cd /usr/local/
[root@taotao2016 local]# ls
bin  etc  games  include  lib  lib64  libexec  nginx-1.11.3  python3  sbin  share  src
[root@taotao2016 local]# cd nginx-1.11.3/
[root@taotao2016 nginx-1.11.3]# ls
conf  html  logs  sbin
[root@taotao2016 nginx-1.11.3]# cd sbin/
[root@taotao2016 sbin]# ls
nginx
[root@taotao2016 sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx-1.11.3/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.11.3/conf/nginx.conf test is successful
直接执行bin下面的nginx就启动了服务
[root@taotao2016 sbin]# netstat -anlpt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4801/nginx: master

访问下试试:

nginx的记录配置心得_第1张图片

经验:

ln -s nginx-1.11.3 nginx  #建立个软连接 以后升级后直接nginx指向新版本就行了

#安装的时候要其他模块请直接看官方的说明文档

二:配置nginx的rewrite

这个模块就是安装的时候用的pcre软件提供的

rewrite指定语法; rewrite regex replacement[flag]   应用位置 server、location 、if

该指令根据表达式来重定向URI,或者修改字符串。指令根据配置文件中的顺序来执行。注意重写表达式只对相对路径有效。如果你想配对主机名,你应该使用if语句,示例如下:

先说后面的flag 的几个说明吧:

1.last     相当于apache里面的[L]标记,表示rewrite。
2.break本条规则匹配完成后,终止匹配,不再匹配后面的规则。
3.redirect  返回302临时重定向,浏览器地址会显示跳转后的URL地址。
4.permanent  返回301永久重定向, 浏览器地址会显示跳转后的URL地址

例子: rewrite ^/(.*) http://www.daxia.help/$1 permanent;      #rewrite是关键字  regex部分^/(.*) 这是一个正则表达式,表示匹配所以,匹配成功后跳转到后面的 replacement部分也就是www.daxia.help这个域名  $1是引用前面()里面的内容简化写入的,permanent是301永久跳转,这是告诉搜索引擎的。

http://www.linuxidc.com/Linux/2014-01/95493.htm  这里有详细配置,我今天有点困了,改天再补详细点

三:日志配置

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

log_format 是定义日志格式的关键字  main是标签 remote_addr 是记录访问客户端的地址  remote_user 远程访问客户端的名称  time_local是日期  request 是http起始行的信息

status http的状态码 200 404 等  body-bytes_set 是服务器发送给客户端的响应body字节数 http_referer 记录这次访问是从那个链接访问过来的用于防盗链设置

http_user_agent 是记录浏览器的信息 比如是手机还是谷歌浏览器 360浏览器等  http_x_forwarded_for 当有代理时候设置这个记录客户端的真实地址用的

access_log 配置   语法: access_log path[format[buffer=size[flush=time]] [if=condition];     access_log path format gzip [=level] [buffer=size][flush=time][if=condition]  ;   access_log syslog:server=address[ paarmeter=value][format[if=condition]];

buffer=size 是存放访问日志的缓冲区大小,flush=time是日志多久刷新到硬盘的时间 ,gzip[level] 表示压缩级别 [if=condition] 表示其他条件 一般这些都不需要配置

access_log off 是关闭记录日志  可以应用到 http server location \if in locaton\limit_except中

例子: access_log logs/access_www.log main gzip buffer=32k flush=5s;

http://www.linuxidc.com/Linux/2014-12/110989.htm  日志切割

访问日的切割:

#!bin/sh
Dateformat=`date +%Y%m%d`
Basedir="/usr/local/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload

##就是把日志文件移动到另外个文件在重启加载下nginx的配置 重新生成日志 最后把脚本放到crontab里面定时执行就行了

四、location的配置

nginx location语法

基本语法:location [=|~|~*|^~] /uri/ { … }

= 严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求。

~ 为区分大小写匹配(可用正则表达式)

~* 为不区分大小写匹配(可用正则表达式)

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配

^~ 如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式。

http://www.cnblogs.com/lidabo/p/4169396.html

http://www.linuxidc.com/Linux/2015-06/119398.htm

五:nginx的一些简单优化

去掉版本信息: 

在http标签里面加入server_tokens off; 开启或者关闭 on默认的

要是修改源码的配置就是 nginx-1.11.3/src/core/nginx.h里面 

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Nginx, Inc.
 */


#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_


#define nginx_version      1011003
#define NGINX_VERSION      "1.11.3"     把这里修改了就行修改成自己喜欢的版本 2.22.6比如
#define NGINX_VER          "nginx/" NGINX_VERSION

#ifdef NGX_BUILD
#define NGINX_VER_BUILD    NGINX_VER " (" NGX_BUILD ")"
#else
#define NGINX_VER_BUILD    NGINX_VER
#endif

#define NGINX_VAR          "NGINX"   ###这里也能修改成其他软件比如叫小芳软件 xiaofang
#define NGX_OLDPID_EXT     ".oldbin"


#endif /* _NGINX_H_INCLUDED_ */

还有个文件是 nginx-1.11.3/src/http/ngx_http_header_filter_module.c

static char ngx_http_server_string[] = "Server: nginx" CRLF;  ##把这行的nginx换成自己的名字

最后一个文件是 ngx_http_special_response.c 也在上面的目录:

"


" NGINX_VER "
" CRLF  #这个换成 "
" NGINX_VER (www.daxia.help)"
" CRLF

"


nginx
" CRLF   #换成 "
xiaofang
" CRLF

之后重新编译安装就okl 


修改默认的nobody 用户用你知道的nginx用 


优化nginx的worker进程数:

worker_processes 1; 指定nginx 开启的进程数一般是cpu核数的* (1-2)


优化绑定不同的nginx进程到不同的cpu上:

例如:

worker_processes     4;

worker_cpu_affinity 0001 0010 0100 1000;

可以查看官方的说明


nginx 的处理模型优化:  

一般都是linux 选择epoll就行了,BSD系统用kqueue 可以看官网的说明

调整nginx单个进程如许打开的客户端最大连接数

worker_connections 20480 

# 默认1024 公式 Max_client=worker_processes*worker_connections 进程的最大连接数受linux系统的最大打开文件数限制 ulimit -HSn 65535 


配置nginx worker 进程最大打开文件数

worker_rlimit_nofile number  ;

worker_rlimit_nofile 65535; ulimit -HSn 设置一样就行


优化配置服务器域名的散列表大小:

server_names_hash_bucket_size 64;

server_names_hash_max_size  128;


开启高效的文件传输:

sendfile on


优化连接参数。调整超时时间

keepalive_timeout 75s ;默认 

上传文件大小的设置:

client_max_body_size 8m;


先到这把 困了!