nginx

nginx

Nginx

--prefix=/usr/local/nginx 设置nginx安装路径
--conf-path=/etc/nginx/nginx.conf 设置nginx主配置文件路径
--user=nginx --group=nginx 设置nginx普通用户运行
--error-log-path=/var/log/nginx/error_log 错误日志输入路径
--http-log-path=/var/log/nginx/access_log 访问日志输出路径
--pid-path=/var/run/nginx/nginx.pid 设置PID路径
--lock-path=/var/lock/nginx.lock 设置lock路径
--with-http_ssl_module 开启SSL模块
--with-http_stub_status_module 开启状态页面
--with-http_gzip_static_module 开启gz模块,压缩静态页面
--with-http_flv_module 开启流媒体
--with-http_mp4_module 开启MP4模块

编译安装

yum install -y pcre-devel openssl-devel
useradd -r ngix
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --error-log-path=/var/log/nginx/error_log --http-log-path=/var/log/nginx/access_log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module
make -j4
make install
ln -s /usr/local/

/usr/local/nginx/sbin/nginx
ss -tnlp|grep 80

主配置段的指令:

正常运行的必备配置:

1、user USERNAME[GROUPNAME];
指定运行worker进程的用户和组;user nginx nginx;

2、pid/path/to/pid_file;
指定nginx守护进程的pid文件;pid/var/run/nginx/nginx.pid;

3、worker_rlimit_nofile#;
指定所有worker进程所能够持开的最大文件句柄数;

性能相关优化相关配置

1、worker_processes #;
worker进程的个数;通常应该略少于CPU物理核心数;
#通常值为物理CPU核心数-1或-2
#常用

2、worker_cpu_affinity cpumask.…;
#一个CPU核心只运行一个NGINX进程,一个进程专用一个CPU核心
优点:提升缓存的命中率;
context switch:会产生CPU的不必要的消耗;
cpumask: 0000 0000
0000 0001
0000 0010
0000 0100
0000 1000
worker_cpu affinity 00000001 00000010 00000100;
#常用
#worker_cpu affinity auto;

3、timer_resolution
计时器解析度:降低此值,可减少gettimeofday()系统调用的次数:
#解析次数越高,精度越高,资源消耗越大,
#解析次数越低,精度越低,若要求不是特别是高的话,可减少解析数

4、worker_priority number;
指明worker进程的nice值;
-20,19
100,139
数字越小,优先级高
#必须时,可降低
#worker_priority -10;

事件相关的配置:

1、accept_mutex {off|on};
master调度用户请求至各worker进程时使用的负载均衡锁;
on表示能让多个worker轮流地、序列化地去响应新请求:
#当一个新的用户请求到达时,主控进程必须重多个worker进程挑选一个回应请求
#若开启,公平的,轮流,序列的分配,若关闭,则随机

2、lock_file file;
accept_mutex用到的锁文件路径;
#利用这个函数,要对文件进行读写的一个应用程序可将文件的某一部分锁定起来,使其不能由其他应用程序访问。这样便避免了同时读写时发生的冲突。

3、use [epoll|rtsig|select|poll];
指明使用的事件模型:建议让nginx自行选择;

4、worker_connections #:
设定单个worker进程所能够处理的最大并发连接数量;

#worker_connections 10240;
#承受最大并发数worker_connections * work_processes
#大于65535,就无效,若想有效,修改内核参数
#此选项一般必须设置的;

用户调试、定位问题

#编译时添加此选项,才可以调试
--with-debug

1、daemon {on|off};
是否以守护进程方式运行nginx;调试时应该设置为off
#关闭时,可在控制台看到调试输出信息

2、master_process {on|off};
是否以master/worker模型来运行nginx;高度时可以设置为off

3、error_log file | syslog:server=address[,parameter=value]| memory:size [debug | info | notice | warn | error | crit | alert | emerg];
error_log 位置 级别;
若要使用debug级别,需要在编译nginx时使用了--with-debug选项:

总结:经常需要进行调整的参数

worker_processes
worker_connections
worker_cpu_affinity
worker_priority

新改动配置生效的方式:
nginx -s reload
stop,quit,reopen

nginx作为web服务器时使用的配置
http {}:由ngx_http_core_module模块所引入
配置框架
http {
upstream {
...
}

server {
	location URL{
		root "/path/to/somedir";
		...
	} # 类似于http中的<Location>,用于定义url与本地文件系统的映射关系
	location URL {
		if ...{
			...
		}
		...
	}
	...
} #每个server 类似于http中的一个<VirtualHost>

server {
	...
}

}

注意:与http相关的指令仅能够旋转于http、server、location、upstream、if上下文,但有些指令仅应用于这5种上下文中的某些种;

配置指令:

1、server { }
定义一个虚拟主机

示例
server{
1isten 8080;
server_name www.test.com;
root"/vhosts/web1";
}

2、listen
指定监听的地址和端口:
以下两种方式都可以
listen address [:port];
listen port;

3、server_name NAME [...];
后面可以跟多个主机名[域名]:名称还可以使用正则表达式(~)或通配符;

(1)精确匹配优先 www.test.com
(2)左侧通配符匹配检查: .test.com
(3)右侧通配符匹配检查: mail.
(4)正则表达式匹配检查: ~^.
.test.com$
(5)default_server;

4、root path;
设置资源路径映射:用于指明请求的url所对应资源所在文件系统上的起始路径
root "/var/www/html";

5、location [ = | ~ | ~* | ~~ ] uri { … }
location @name {...}
功能:允许根据用户请求的URI来匹配定义的各location:匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制访问控制等功能;

=:精确匹配检查;
~:正则表达式模式匹配检查,区分字符大小写;
~*:正则表达式模式匹配检查,不区分字符大小写;
^~:URI的前半部分匹配,不支持正则表达式;

匹配的优先级:精确匹配=、^、~*、不带任务符号的

server {
    listen 80;
    server_name www.test.com;
    location / {
        root "/var/www/html/web1";
    }
    
    location /images {
        root "/var/www/html/images";
    }
    
    location ~* \.php$ {
        fagipass ;
    }
}
server{
listen 8080; 
server_name www.test.com;

 location/{
root "/vhosts/webl";
}

 location /images/{
root "/vhosts/images";
}

 location ~* \.(txtltext)${
root "/vhosts/text";
}

6、alias path;
用于location配置段,定义路径别名

1ocation /images/ {
root"/vhosts/web1";
}

 http://ww.test.com/images/a.jpg<--/vhosts/web1/images/a.jpg 
 
location /images/ {
alias"/www/pictures/"; 
}
http://www.test.com/images/a.jpg<--/ww/picuter/a.jpg

注意:root表示指明路径为对应的location "/" URL;alias表示路径映射,即1ocation指令后定义的URL是相对于alias所指明的路径而言:
#使用alias右侧一定要加/

7、index file;
默认主页面

index index.php index.html;

8、error_page code [...] =code URI | @name
根据http响应状态码来指明我用的错误页面;

error_page 404 /404_customed.html

示例 404报错打开404.html, =200修改响应码

error_page 404 =200 /404.html;

9、基于IP的访问控制
allow IP/Network;
deny IP/Network;

示例

allow 192.168.2.0/24;
deny all;

#deny 192.168.2.254;

10、基于用户的访问控制
basic,digest;

auth_basic "heloo vip";
auth_basic_user_file "/path/to/password_file";
账号密码文件建议使用htpasswd来创建;
mkdir /etc/nginx/users
htpasswd -c -m /etc/nginx/users/.htpasswd admin

11、https服务
生成私钥,生成请收签署请求,并获得证书;

cd /etc/pki/CA/
`umask 077;openssl genrsa -out private/cakey.pem 2048`
openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
touch serial index.txt
echo 01 > serial 

mkdir /etc/nginx/ssl
cd /etc/nginx/ssl
`umask 077; openssl genrsa -out nginx.key 1024`
openssl req -new -key nginx.key -out nginx.csr
openssl ca -in nginx.csr -out nginx.crt -days 365

server {
        listen       443 ssl;
        server_name  www.test.com;
        ssl_certificate      /etc/nginx/ssl/nginx.crt;
        ssl_certificate_key  /etc/nginx/ssl/nginx.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

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

12、stub_status { on|off };
仅能用于location上下文

  location /status {
      stub_status on;
      allow 192.168.2.0/24;
      deny all;
		}

示例
Active connections: 1 #当前所有打开状态的连接数
server accepts handled requests
30 30 53
#已经接受的连接数
#已经处理过的连接数
#处理连接的请求,在保持连接模式下,请求数量可能会多于连接数量
Reading: 0 Writing: 1 Waiting: 0
#Reading:正处于接收请求状态的连接数:
#Writing:请求已经接收完成,正处于处理请求或发送响应的过程中的连接数:
#Waiting:保持连接模式,且处于活动状态的连接数:

13、rewrite regex replacement flag;
flag
last:一旦此rewrite规则重写完成后,就不再被后面其它的rewrite规则进行处理;而是由User Agent重新对重写后的URL再一次发起请求,并从头开始执行类似的过程
break:一旦此rewrite规则重写完成后,由User Agent对新的URL重新发起请求,且不再会被当前1ocatrion内的任何rewrite规则所检查:
redirect:以302响应码(临时重定向)返回新的URL:
permanent:以301响应码(永久重定向)返回新的URL:

14、if
语法:if(condition) {...}
应用环境:server,location
condition:
变量名:
变量值为空串,或者以"0"开始,则为false,其它的均为true;

以变量为操作数构成的比较表达式
可使用=,!=类似的比较操作符进行测试:

正则表达式的模式匹配操作
~:区分大小写的模式匹配检查
*:不区分大小写的模式匹配检查!和l~*:对上面两种测试取反
#影响CPU性能

测试路径为文件存在性:-f,!-f

测试指定路径为目录的可能性:-d,!-d

测试文件的存在性:-e,!-e

检查文件是否有执行权限:-x,!-x

示例
if ($http_user_agent ~* MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}

15、防盗链

location ~* `\.(jpg|gif|jpeg|png$) {
    valid_referer none blocked www.test.com;
    if ($invalid_referer) {
        rewrite ^/ http://www.test.com/403.html;
    }
}

16、定制访问日志格式

log_format main '$server_addr $remote_addr [$time_local] $msec+$connection'
                '"$request" $status $connection $request time $body_bytes_sent "$http_referer"'
                '"$http_user_agent" "$http_x_forwarded_for"';

access_log  logs/access.log  main;

注意:此处可用变量为nginx各模块内建变量:

网络连接相关的配置:
1、keepalive_timeout#;
长连接的超时时长,默认75s;
2、keepalive_requests#;
在一个长连接上所能够允许请求的最大资源数:
3、keepalive_disable[msie6lsafarilnone];
为指定类型的User Agent禁用长连接:
4、tcp_nodelay on|off;
是否对长连接使用TCP_NODELAY选项:
#将小的包合并成一个包
#影响用户体验
5、client_header_timeout #;
读取http请求报文首部的超时时长;
6、client_body_timeout #;
读取http请求报文body部分的超时时长:
7、send_timeout #;
发送响应报文的超时时长:

nginx代理

#ngx_http_proxy_module,ngx_http_upstream_module
#ngxhttp_proxy_module:实现反向代理及缓存功能
#proxy_pass http://{SERVER_IP|UPSTREAM_NAME}/uri

l

location / {
proxy_pass http://192.168.2.254; 
proxy_set_header Host $host; 
#记录客户端的主机,不记录代理服务器的主机
proxy_set_header X-Real-IP $remote_addr;
#记录客户端的IP地址,不记录代理服务器的IP
}

代理缓存

mkdir -pv /nginx/cache
chown -R nginx.nginx /nginx/cache

proxy_cache_path /nginx/cache levels=1:2 keys_zone=mycache:500m inactive=1d max_size=10g;
#proxy_cache_path path [levels=levels] keys_zone=name:size [Inactive=time] [max size=size]
#在http{} 缓存存放路径 levels参数定义高速缓存的层次结构级别:从1到3,每个级别接受值1或2. 缓存名字和内存空间大小 缓存的数据超过1天没有被访问就自动清除 访问的缓存数据,硬盘缓存空间大小为10G

proxy_cache mycache;
proxy_cache_valid 200 1d;
proxy_cache_valid 301 302 10m;
proxy_cache_valid any 1m; 
proxy_cache_use_stale error timeout
#在http,server,location{}中
#proxy_cache zone_name
#设置缓存名称
proxy_cache_valid [code] time;
#响应码缓存的时间
proxy_cache_method GET | HEAD | POST
#如果此指令中列出了客户端请求方法,则将缓存响应。
#设置缓存哪些 HTTP 方法, 默认缓存 HTTP GET/HEAD 方法, 不缓存 HTTP POST 方法
proxy_cache_use_stale
#确定在与代理服务器通信期间可以在哪些情况下使用过时的缓存响应
#error如果无法选择代理服务器来处理请求,则该参数还允许使用陈旧的缓存响应。
#此外,updating如果当前正在更新,该参数允许使用过时的缓存响应。这允许在更新缓存数据时最小化对代理服务器的访问次数
proxy_cache_min_uses number
#某响应被请求多少次后,才清除缓存
proxy_cache_bypass string
#定义不从缓存中获取响应的条件。如果字符串参数的至少一个值不为空且不等于 “0”,则不会从缓存中获取响应:
#设置在何种情形下nginx将不从cache取数据的;
proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;
proxy_cache_bypass $http_pragma $http_authorization;
proxy_set_header
location / {
proxy_pass http://192.168.2.254; 
proxy_set_header Host $host; 
#记录客户端的主机,不记录代理服务器的主机
proxy_set_header X-Real-IP $remote_addr;
#记录客户端的IP地址,不记录代理服务器的IP
}
#允许将字段重新定义或附加到传递给代理服务器的请求标头该value可以包含文本,变量,以及它们的组合。当且仅当proxy_set_header在当前级别上没有定义指令时,这些指令才从先前级别继承

ngx_http_upstream_module
定义服务器组
proxy_pass,fastcgi_pass,uwsgi_pass
#默认情况下,使用加权循环平衡方法在服务器之间分配请求

upstream back1 {
    server back1.test.com;
    server 192.168.2.100:8080;
}

snat模式的大量client
基于sticky实现session绑定
#nginx版本1.5.7之前用 sticky_cookie_insert;之后用sticky
cookie
#使用该cookie方法时,有关指定服务器的信息将在 nginx 生成的 HTTP

upstream backend {
    server backend1.example.com;
    server backend2.example.com;
    sticky cookie srv_id expires=1h domain=.example.com path=/;
}

route
#使用该route方法时,代理服务器会在收到第一个请求时为客户端分配路由。来自此客户端的所有后续请求将在 cookie 或 URI 中携带路由信息。将此信息与服务器指令的 “ route” 参数进行比较,以标识应将请求代理到的服务器。如果未指定 “ ” 参数,则路由名称将是 IP 地址和端口的 MD5 哈希值或 UNIX 域套接字路径的十六进制表示形式。如果指定的服务器无法处理请求,则配置的平衡方法将选择新服务器,就好像请求中没有路由信息一样。

upstream backend {
    server backend1.example.com route=a;
    server backend2.example.com route=b;
    sticky route $route_cookie $route_uri;
}

learn
#当使用learn方法(1.7.1)时,nginx 分析上游服务器响应并学习通常在HTTP cookie 中传递的服务器启动的会话。
这种性能较好

upstream backend {
   server backend1.example.com:8080;
   server backend2.example.com:8081;

   sticky learn
          create=$upstream_cookie_examplecookie
          lookup=$cookie_examplecookie
          zone=client_sessions:1m;
}

least_conn:高度方法,最少连接

keepalive
upstream memcached_backend {
    server 127.0.0.1:11211;
    server 10.0.0.2:11211;
    keepalive 32;
}

短链接模式,拿到数据断开连接
不建议开启

health_check;
#状态检测
建议:关闭访问日志;

http {
    server {
    ...
        location / {
            proxy_pass http://backend;
            health_check match=welcome;
        }
    }

    match welcome {
        status 200;
        header Content-Type = text/html;
        body ~ "Welcome to nginx!";
    }

#status is not one of 301, 302, 303, or 307, and header does not have "Refresh:"

match not_redirect {
    status ! 301-303 307;
    header ! Refresh;
}
# status ok and not in maintenance mode
match server_ok {
    status 200-399;
    body !~ "maintenance mode";
}

自定义响应首部

add_header X-Via $server_addr;
add_header test $server_addr;
add_header X-Cache $upstream_cache_status;

curl -I [domain|IP]

fastcgi的相关配置
lnmp:php启用fpm模型

location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    include        fastcgi_params;
}

注意:Nginx 访问 PHP 文件的 File not found 错误处理,两种情况
这个错误很常见,原有有下面两种几种
php-fpm 找不到 SCRIPT_FILENAME 里执行的 php 文件
php-fpm 不能访问所执行的 php,也就是权限问题

  • 第一种情况
更改配置文件 nginx.conf 
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 
替换成下面
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
然后重新加载 nginx 配置文件 
/etc/init.d/nginx reload
  • 第二种情况
两种解决方法: 
第一种,就是把你 root 文件夹设为其他用户允许 
第二种,找到你的 php-fpm 的配置文件,找到下面这段,把 apache 替换成你要的用户组
; RPM: apache Choosed to be able to access some dir as httpd 
user = apache 
; RPM: Keep a group allowed to write in log dir. 
group = apache

php测试页面

<?php
phpinfo();
?>

php-mysql测试页面

<?php
$conn =mysql_connect('127.0.0.1','root','');
if ($conn)
   echo succ;
else
   echo error;
 mysql_close();
?>

三个问题
1、root为同一路径
2、root为不同的路径
3、fpm server为另一主机
fastcgi_pass fastcgi://192.168.2.254:9000

vim /etc/php-fpm.d/www.conf
listen = 192.168.2.254:9000
listen.allowed_clients = 192.168.2.100
user = apache
group = apache
chown apache.apahce /html/emlog/

http://nginx.org
http://tengine.taobao.org

#指定运行worker进程的用户和组;user nginx nginx 可不设置
user nginx nginx;
#worker进程的个数;通常应该略少于CPU物理核心数;
#通常值为物理CPU核心数-1-2
#常用
worker_processes auto;
#cpu 亲和力配置,让不同的进程使用不同的 cpu
#一个CPU核心只运行一个NGINX进程,一个进程专用一个CPU核心
#优点:提升缓存的命中率;
#常用
worker_cpu_affinity 0001 0010 0100 1000;
#最大文件打开数(连接),可设置为系统优化后的 ulimit -HSn 的结果
#指定所有worker进程所能够持开的最大文件句柄数;
worker_rlimit_nofile 51200;
#减少计时器解析度的值 < 间隔 >。
#解析次数越高,精度越高,资源消耗越大,
#解析次数越低,精度越低,若要求不是特别是高的话,可减少解析数
#timer_resolution 间隔; 可减少 gettimeofday() 系统调用的次数;
#调低 worker 线程的 nice 值, 提高优先级。#必须时,可降低
#worker_priority -10;        指明 worker 进程的 nice 值;
#错误日志存放目录
#error_log  /var/log/nginx/error_log;
#访问日志存放目录
#access_log /var/log/nginx/access_log;
#指定nginx守护进程的pid文件
#pid        /var/run/nginx/nginx.pid;
#工作模式及连接数上限
events {
#epoll 是多路复用 IO(I/O Multiplexing) 中的一种方式, 但是仅用于 linux2.6 以上内核, 可以大大提高 nginx 的性能
    use epoll;
#单个后台 worker process 进程的最大并发链接数
    worker_connections  51200;
}

http {
#加载配置文件
#include /usr/loca/nginx/conf/lvs.conf;
####
#隐藏响应 header 和错误通知中的版本号
      server_tokens off;
#文件扩展名与类型映射表
    include       mime.types;
#limit 模块,可防范一定量的 DDOS 攻击
#用来存储 session 会话的状态,如下是为 session 分配一个名为 one 的 10M 的内存存储区,限制了每秒只接受一个 ip 的一次请求 1r/s
#    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
#    limit_conn_zone $binary_remote_addr zone=addr:10m;
#默认文件类型
    default_type  application/octet-stream;
    log_format  main    '$server_addr $remote_addr [$time_local] $msec+$connection'
                        '"$request" $status $connection $request_time $body_bytes_sent "$http_referer"'
                        '"$http_user_agent" "$http_x_forwarded_for"';
    open_log_file_cache max=1000 inactive=20s min_uses=1 valid=1m;
    access_log /var/log/nginx/access_log main;
    log_not_found on;
#开启高效传输模式
    sendfile        on;
#将小的包合并成一个包
#激活 tcp_nopush 参数可以允许把 httpresponse header 和文件的开始放在一个文件里发布,
#积极的作用是减少网络报文段的数量
#选项仅在使用 sendfile 的时候才开启。
#socket 有包就发,管他大小,很直接暴力。
#官方#将 tcp_nopush指令与指令一起使用。这使得 NGINX 可以在获得数据块之后立即在一个数据包中发送 HTTP 响应头。sendfile on;sendfile()
   tcp_nopush     on;
#激活 tcp_nodelay,内核会等待将更多的字节组成一个数据包,从而提高 I/O 性能
#官方#该 tcp_nodelay指令允许覆盖 Nagle 的算法,该算法最初设计用于解决慢速网络中小数据包的问题。该算法将许多小数据包合并为一个较大的数据包,并以200 ms 延迟发送数据包。如今,在提供大型静态文件时,无论数据包大小如何,都可以立即发送数据。延迟也会影响在线应用程序(ssh,在线游戏,在线交易等)。默认情况下,tcp_nodelay指令设置为on,这意味着禁用 Nagle 的算法。仅将此指令用于 keepalive 连接:
#Nagle 和 DelayedAcknowledgment 的延迟问题
#Nagle:假如需要频繁的发送一些小包数据,比如说 1 个字节,以 IPv4 为例的话,则每个包都要附带 40 字节的头,也就是说,总计 41 个字节的数据里,其中只有 1 个字节是我们需要的数据。
#为了解决这个问题,出现了 Nagle 算法。它规定:如果包的大小满足 MSS,那么可以立即发送,否则数据会被放到缓冲区,等到已经发送的包被确认了之后才能继续发送。通过这样的规定,可以降低网络里小包的数量,从而提升网络性能。
#DelayedAcknowledgment:假如需要单独确认每一个包的话,那么网络中将会充斥着无数的 ACK,从而降低了网络性能。为了解决这个问题,DelayedAcknowledgment 规定:不再针对单个包发送 ACK,而是一次确认两个包,或者在发送响应数据的同时捎带着发送 ACK,又或者触发超时时间后再发送 ACK。通过这样的规定,可以降低网络里 ACK 的数量,从而提升网络性能。
#Nagle 和 DelayedAcknowledgment 虽然都是好心,但是它们在一起的时候却会办坏事
#tcp_nodelay 为什么只在 keep-alive 才启作用
#启用 Nagle 算法 小包来了,一直等着,直到凑成满的才发。到数据流的最后一段了,nginx 直接去掉了 tcp_nopush,最后一个小包立刻就发出去了,就避免了 200ms 的一个延迟。
#将多个小的包合并成一个报文
#关闭则影响用户体验,延迟
    tcp_nodelay on;
#开启 gzip 压缩功能
    gzip  on;
#设置允许压缩的页面最小字节数,页面字节数从 header 头的 Content-Length 中获取。默认值是 0,表示不管页面多大都进行压缩。建议设置成大于 1K。如果小于 1K 可能会越压越大。
    gzip_min_length  1k;
#压缩缓冲区大小。表示申请 4 个单位为 16K 的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储 gzip 压缩结果。
    gzip_buffers     4 16k;
#压缩版本(默认 1.1,前端为 squid2.5 时使用 1.0)用于设置识别 HTTP 协议版本,默认是 1.1,目前大部分浏览器已经支持 GZIP 解压,使用默认即可。
#    gzip_http_version 1.0;
#压缩比率。用来指定 GZIP 压缩比,1 压缩比最小,处理速度最快;9 压缩比最大,传输速度快,但处理最慢,也比较消耗 cpu 资源。
    gzip_comp_level 6;
#用来指定压缩的类型,“text/html” 类型总是会被压缩
    gzip_types       text/plain application/x-javascript text/css application/xml;
#连接超时时间,单位是秒
#vary header 支持。该选项可以让前端的缓存服务器缓存经过 GZIP 压缩的页面,例如用Squid 缓存经过 Nginx 压缩的数据。
    gzip_vary off;
#开启 ssi 支持,默认是 off
#  ssi on;
#  ssi_silent_errors on;
#长连接的超时时长,默认75
    keepalive_timeout  60;
#在一个长连接上所能够驴行请求的最大资源数
#keepalive_requests 
#设置日志模式
#反向代理负载均衡设定部分
#upstream 表示负载服务器池,定义名字为 backend_server 的服务器池
upstream back_server {
#设置由 fail_timeout 定义的时间段内连接该主机的失败次数,以此来断定 fail_timeout 定义的时间段内该主机是否可用。默认情况下这个数值设置为 1。零值的话禁用这个数量的尝试。
#设置在指定时间内连接到主机的失败次数,超过该次数该主机被认为不可用。
#这里是在 30s 内尝试 2 次失败即认为主机不可用!
    server   192.168.2.100:80 weight=1 max_fails=2 fail_timeout=30s;
    server   192.168.2.101:80 weight=1 max_fails=2 fail_timeout=30s;
#    server   192.168.2.102:80 weight=1 max_fails=2 fail_timeout=30s;
#    server   192.168.2.103:80 weight=1 max_fails=2 fail_timeout=30s;
    }
#基于域名的虚拟主机
    server{
          listen 80; #监听端口
#          server_name www.back_servers.com;
#          location / {
#          root html; #站点根目录,即网站程序存放目录
#          index index.php index.html; #首页排序
#          }
          
      location / {
          proxy_pass http://back_server; 
          #允许将字段重新定义或附加到传递给代理服务器的请求标头该value可以包含文本,变量,以及它们的组合。
          #当且仅当proxy_set_header在当前级别上没有定义指令时,这些指令才从先前级别继承
          proxy_set_header Host $host; #记录客户端的主机,不记录代理服务器的主机       
          proxy_set_header X-Real-IP $remote_addr ; #记录客户端的IP地址,不记录代理服务器的IP
          #proxy_cache_path path [levels=levels] keys_zone=name:size [Inactive=time] [max size=size]
          #在http{} 缓存存放路径 levels参数定义高速缓存的层次结构级别:从13,每个级别接受值12. 缓存名字和内存空间大小 缓存的数据超过1天没有被访问就自动清除 访问的缓存数据,硬盘缓存空间大小为10G
          #proxy_cache_path /nginx/cache levels=1:2 keys_zone=mycache:500m inactive=1d max_size=10g;
          #proxy_cache mycache; #设置缓存名称
          #响应码缓存的时间
          #proxy_cache_valid [code] time;
          #proxy_cache_valid 200 1d;
          #proxy_cache_valid 301 302 10m;
          #proxy_cache_valid any 1m; 
          #如果此指令中列出了客户端请求方法,则将缓存响应。
          #设置缓存哪些 HTTP 方法, 默认缓存 HTTP GET/HEAD 方法, 不缓存 HTTP POST 方法
          ##proxy_cache_method GET | HEAD | POST
          #确定在与代理服务器通信期间可以在哪些情况下使用过时的缓存响应
          #error如果无法选择代理服务器来处理请求,则该参数还允许使用陈旧的缓存响应。
          #此外,updating如果当前正在更新,该参数允许使用过时的缓存响应。这允许在更新缓存数据时最小化对代理服务器的访问次数
          ##proxy_cache_use_stale error timeout
          #某响应被请求多少次后,才清除缓存
          ##proxy_cache_min_uses number
          #定义不从缓存中获取响应的条件。如果字符串参数的至少一个值不为空且不等于 “0”,则不会从缓存中获取响应:
          #设置在何种情形下nginx将不从cache取数据的;
          ##proxy_cache_bypass string
#          }
#防盗链          
#      location ~* `\.(jpg|gif|jpeg|png$) {
#          valid_referer none blocked www.test.com;
#          if ($invalid_referer) {
#          rewrite ^/ http://www.test.com/403.html;
#        }
#      }
#启用php
# 		  location ~ \.php$ {
#          root           html;
#          fastcgi_pass   192.168.2.254:9000; #监听本机 9000 端口
#          fastcgi_index  index.php; #设定动态首页
#若访问php页面not found 修改下面的选项
#          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#          include        fastcgi_params;
#}
#若php服务分离出来,需要把访问问点目录所有者、所属组改成apache
### grep apache /etc/php-fpm.d/www.conf 
###user = apache
###group = apache
###php监听IP和端口
###grep 192.168.2.254 /etc/php-fpm.d/www.conf
###listen = 192.168.2.254:9000
###php允许监听的IP
###grep 192.168.2.100 /etc/php-fpm.d/www.conf
###listen.allowed_clients = 192.168.2.100
#          location ~ \.php$ {
#          fastcgi_pass   192.168.2.254:9000;
#          fastcgi_index  index.php;
#          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#          include        fastcgi_params;
#                }          
##add by 20140321#######nginx 防 sql 注入 ##########

          }
          
#		  location ~ ^/admin.php {
#            location ~ \.php$
#             {
#              fastcgi_pass  127.0.0.1:9000;
#              fastcgi_index index.php;
#              include fcgi.conf;
#             }
#            allow   192.168.2.254;
#            deny    all; #禁止其他 ip 访问
#            }

      location /yum {
          access_log off;
          autoindex on;   #开启 nginx 目录浏览功能
          autoindex_exact_size off;   #文件大小从 KB 开始显示
          autoindex_localtime on;   #显示文件修改时间为服务器本地时间
      }
#将符合 js,css 文件的等设定 expries 缓存参数,要求浏览器缓存。
      #location~ .*\.(js|css)?$ {
      # expires      30d; #客户端缓存上述 js,css 数据 30 天
      #}
}
    server {
          listen 8888;
          server_name nginx-status;
          location / {
          access_log off;
          deny all;
          return 503;
          }
          location /status {
          stub_status on;
          access_log off;
       	  #error_page 404 =200 /404.html;
            #auth_basic "welcome vip";
            #建立认证
            #auth_basic_user_file /etc/nginx/users/.htpasswd;
            allow 127.0.0.1;
            allow 192.168.2.0/24;
            access_log off; #不记录访问日志
            deny all; #拒绝所有用户
               }
      }

#HTTPS server
#  server {
#      listen       443 ssl;
#      server_name  www.test.com;
#     charset utf-8; #gbk,utf-8,gb2312,gb18030 可以实现多种编码识别
#      ssl_certificate      /etc/nginx/ssl/nginx.crt; #服务的证书
#      ssl_certificate_key  /etc/nginx/ssl/nginx.key; #服务端 key
#      ssl_session_cache    shared:SSL:1m;
#      ssl_session_timeout  5m;#session 超时时间
#      ssl_ciphers  HIGH:!aNULL:!MD5;#加密算法
      #ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; 
#      ssl_prefer_server_ciphers  on;#启动加密算法
#      #access_log /var/log/nginx/acess_log;
#      #error_log /var/log/nginx/error_log;
  
#      location / {
#            root   html;
#            index  index.html index.htm;
#        }
#  }
}


服务脚本

#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=/etc/$NAME/$NAME.conf
PIDFILE=/var/run/$NAME/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# source function library
. /etc/rc.d/init.d/functions

set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
    echo -n "Starting $DESC: $NAME"
    do_start
    echo "."
;;
stop)
    echo -n "Stopping $DESC: $NAME"
    do_stop
    echo "."
;;
reload|graceful)
    echo -n "Reloading $DESC configuration..."
    do_reload
    echo "."
;;
restart)
    echo -n "Restarting $DESC: $NAME"
    do_stop
    do_start
    echo "."
;;
*)
    echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

关于证书配置

cd /etc/pki/CA/
`umask 077;openssl genrsa -out private/cakey.pem 2048`

openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HA
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [Default Company Ltd]:TEST
Organizational Unit Name (eg, section) []:OPS
Common Name (eg, your name or your server's hostname) []:CA.TEST.COM
Email Address []:CAADMIN@TEST.COM

touch serial index.txt
echo 01 > serial 

mkdir /etc/nginx/ssl
cd /etc/nginx/ssl
`umask 077; openssl genrsa -out nginx.key 1024`

openssl req -new -key nginx.key -out nginx.csr
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HA
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [Default Company Ltd]:TEST
Organizational Unit Name (eg, section) []:OPS
Common Name (eg, your name or your server's hostname) []:WWW.TEST.COM 
Email Address []:WEB1@TEST.COM  

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

openssl ca -in nginx.csr -out nginx.crt -days 3650

openssl的配置文件:/etc/pki/t1s/openssl.cnf

(1)创建所需要的文件
#touch index.txt
#echo 01>serial

(2)CA自签证书
#(umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
#openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 3650 -out /etc/pki/CA/cacert.pem
-new:生成新证书签署请求:
-x509:专用于CA生成自签证书;
-key:生成请求时用到的私钥文件;
-days n:证书的有效期限:
-out /PATH/TO/SOMECERTFILE:证书的保存路径:

(3)发证
(a)用到证书的主机生成证书请求
#umask077;openssl genrsa-out/etc/httpd/ss1/httpd.key 2048)
#openssl req -new -key /etc/httpd/ssl/httpd.key -days 365 -out /etc/httpd/ss1/httpd.csr
(b)把请求文件传输给CA:
(c)CA签署证书,并将证书发还给请求者:
#openssl ca -in /tmp/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365
查看证书中的信息:
openssl x509 -in /PATH/FROM/CERT_FILE -noout -text|-subject|-serial

(4)吊销证书
(a)客户端获取要吊销的证书的serial
#openssl ×509 -in /PATH/FROM/CERT_FILE -noout -serial -subject
(b)CA先根据客户提交的serial与subject信息,对比检验是否与index.txt文件中的信息一致;吊销证书:
#openssl ca -revoke /etc/pki/CA/newcerts/SERIAL.pem
(c)生成吊销证书的编号(第一次吊销一个证书)
#echo 01 > /etc/pki/CA/crlnumber
(d)更新证书吊销列表
#openssl ca -gencrl -out thisca.crl

查看cr1文件:
#openssl crl -in /PATH/FROM/CRL_FILE.crl -noout -text

CA服务器生成证书及私钥

cat caserver.sh
#/bin/bash

CADIR="${CADIR}"
touch ${CADIR}/index.txt
echo 01 > ${CADIR}/serial
(umask 077;openssl genrsa -out ${CADIR}/private/cakey.pem 2048)
openssl req -new -x509 -key ${CADIR}/private/cakey.pem -days 3650 -out ${CADIR}/cacert.pem
echo "ca server is success"
[root@ansiable CA]# bash ~/caserver.sh 
Generating RSA private key, 2048 bit long modulus
..........................................................+++
.....+++
e is 65537 (0x10001)
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
#国名(2个字母代码)[XX]:CN
State or Province Name (full name) []:SH   
#州或省名(全名)[]:SH
Locality Name (eg, city) [Default City]:SH
#地点名称(例如,城市)[默认城市]:SH
Organization Name (eg, company) [Default Company Ltd]:TEST
#组织名称(例如,公司)[Default Company Ltd]:TEST
Organizational Unit Name (eg, section) []:OSADMIN
#组织单位名称(例如,部分)[]:OSADMIN
Common Name (eg, your name or your server's hostname) []:CA.TEST.COM
#通用名称(例如,您的姓名或服务器的主机名)[]:CA.TEST.COM
Email Address []:CAADMIN@TEST.COM
#电子邮件地址[]:CAADMIN@TEST.COM

web服务器生成证书及私钥发送给CA签署

cat httpssl.sh
#!/bin/bash

#SSLDIR="/etc/httpd/ssl"
SSLDIR="/usr/local/nginx/ssl"
#SERVERNAME="httpd"
SERVERNAME="nginx"

if [ ! -d ${SSLDIR} ];then
mkdir ${SSLDIR};
fi

(umask 077; openssl genrsa -out ${SSLDIR}/${SERVERNAME}.key 2048)
openssl req -new -key ${SSLDIR}/${SERVERNAME}.key -days 365 -out ${SSLDIR}/${SERVERNAME}.csr
echo "${SSLDIR}/${SERVERNAME}.csr put caserver"
[root@web2 ~]# bash httpssl.sh 
Generating RSA private key, 2048 bit long modulus
.....................................................................................................................................+++
.................+++
e is 65537 (0x10001)
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
#国名(2个字母代码)[XX]:CN
State or Province Name (full name) []:SH
#州或省名(全名)[]:SH
Locality Name (eg, city) [Default City]:SH
#地点名称(例如,城市)[默认城市]:SH
Organization Name (eg, company) [Default Company Ltd]:TEST
#组织名称(例如,公司)[Default Company Ltd]:TEST
Organizational Unit Name (eg, section) []:WEBADMIN
#组织单位名称(例如,部分)[]:WEBADMIN
Common Name (eg, your name or your server's hostname) []:WWW.TEST.COM
#通用名称(例如,您的姓名或服务器的主机名)[]:WWW.TEST.COM
Email Address []:WEBADMIN@TEST.COM
#电子邮件地址[]:WEBADMIN@TEST.COM

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
/etc/httpd/ssl/httpd.csr put caserver

CA服务器签署

[root@ansiable ~]# cat casign.sh 
#!/bin/bash

#NAME="httpd"
NAME="nginx"
DIR="/etc/pki/CA/certs"

openssl ca -in /tmp/${NAME}.csr -out ${DIR}/${NAME}.crt -days 365
echo -e "${DIR}/${NAME}.crt sign is ok\n put webssl"

安装nginx脚本
nginx_autoinstall.sh

#!/bin/bash

CPUNUM=`grep "processor" /proc/cpuinfo |wc -l`
NGINX_VER="1.14.0"
NGINX_DIR="/usr/local/nginx"
NGINX_ARGS="--prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error_log --http-log-path=/var/log/nginx/access_log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module"
NGINX_YUM="pcre-devel openssl-devel make gcc gcc-c++"
NGINX_CONFTD="/root"
NAME="nginx"

yum install -y ${NGINX_YUM} &>/dev/null
tar xf ${NAME}-${NGINX_VER}.tar.gz
cd ${NGINX_CONFTD}/${NAME}-${NGINX_VER}
#.${NGINX_CONFTD}/${NAME}-${NGINX_VER}/configure ${NGINX_ARGS}
./configure ${NGINX_ARGS} &>/dev/null
make -j${CPUNUM} &>/dev/null && make install &>/dev/null 

cp /etc/${NAME}/${NAME}.conf{,.bak}
ln -s ${NGINX_DIR}/sbin/${NAME} /sbin/${NAME}
cp ${NGINX_CONFTD}/${NAME}.conf /etc/${NAME}/${NAME}.conf -rf
cp ${NGINX_CONFTD}/${NAME} /etc/init.d/ -rf
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
/etc/init.d/${NAME} restart
ss -tnl
echo 'nginx install succeful'

posted @ 2018-11-12 15:33  Final233  阅读(321)  评论(0编辑  收藏  举报