Nginx系列教程01

本文主要介绍Centos6.x系统安装nginx和nginx基本使用。

1.Nginx下载安装

可以下载ngxin源码包来安装,也可以通过nginx官网的YUM源安装。

  • 配置YUM源

    1
    2
    3
    4
    5
    6
    7
    # vim /etc/yum.repos.d/nginx.repo
    [nginx]
    name=nginx
    baseurl=http://nginx.org/packages/centos/6/x86_64/
    failovermethod=priority
    enabled=1
    gpgcheck=0
  • 安装nginx

1
# yum -y install nginx

2.Nginx配置文件

  • 查看nginx配置文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
  • nginx主配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# vim /etc/nginx/nginx.conf
#运行用户
user nginx;
#工作进程:数目。
worker_processes 1;

# 错误日志:存放路径。
error_log /var/log/nginx/error.log warn;
# pid(进程标识符):存放路径。
pid /var/run/nginx.pid;

# 事件配置
events {
#每个进程可以处理的最大连接数
worker_connections 1024;
}

# http参数
http {
# 文件扩展名与文件类型映射表
include /etc/nginx/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"';

#连接日志的路径,指定的日志格式放在最后
access_log /var/log/nginx/access.log main;

#开启高效传输模式
sendfile on;
#防止网络阻塞
#tcp_nopush on;

#客户端连接超时时间,单位是秒
keepalive_timeout 65;

#客户端请求头读取超时时间
client_header_timeout 10;

#设置客户端请求主体读取超时时间
client_body_timeout 10;

#响应客户端超时时间
send_timeout 10;

#开启gzip压缩输出
#gzip on;

# 子配置文件
include /etc/nginx/conf.d/*.conf;
}
  • 子配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# vim /etc/nginx/conf.d/default.conf
# 虚拟主机定义
server {
# 监听端口
listen 80;
# 访问域名
server_name localhost;

# 编码格式,若网页格式与此不同,将被自动转码
#charset koi8-r;
# 虚拟主机访问日志定义
#access_log /var/log/nginx/host.access.log main;

# 对URL进行匹配
location / {
# 访问路径,可相对也可绝对路径
root /usr/share/nginx/html;
# 首页文件。以下按顺序匹配
index index.html index.htm;
}
# 错误信息返回页面
#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# 访问URL以.php结尾则自动转交给127.0.0.1
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# php脚本请求全部转发给FastCGI处理
# 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_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# 禁止访问.ht页面 (需ngx_http_access_module模块)
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}

# HTTPS虚拟主机定义
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.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;
# }
#}
}
  • 其他配置

​ Nginx状态监控

1
2
3
4
5
6
7
8
9
10
11
12
#Nginx运行状态,StubStatus模块获取Nginx自启动的工作状态(编译时要开启对应功能)
#location /NginxStatus {
# #启用StubStatus的工作访问状态
# stub_status on;
# #指定StubStaus模块的访问日志文件
# access_log logs/Nginxstatus.log;
# #Nginx认证机制(需Apache的htpasswd命令生成)
# #auth_basic "NginxStatus";
# #用来认证的密码文件
# #auth_basic_user_file ../htpasswd;
#}
访问:http://IP/NginxStatus(测试就不加密码验证相关)

​ 反向代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#以下配置追加在HTTP的全局变量中

#nginx跟后端服务器连接超时时间(代理连接超时)
proxy_connect_timeout 5;

#后端服务器数据回传时间(代理发送超时)
proxy_send_timeout 5;

#连接成功后,后端服务器响应时间(代理接收超时)
proxy_read_timeout 60;

#设置代理服务器(nginx)保存用户头信息的缓冲区大小
proxy_buffer_size 16k;

#proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
proxy_buffers 4 32k;

#高负荷下缓冲大小(proxy_buffers*2)
proxy_busy_buffers_size 64k;

#设定缓存文件夹大小,大于这个值,将从upstream服务器传
proxy_temp_file_write_size 64k;

#反向代理缓存目录
proxy_cache_path /data/proxy/cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=1g;

#levels=1:2 设置目录深度,第一层目录是1个字符,第2层是2个字符
#keys_zone:设置web缓存名称和内存缓存空间大小
#inactive:自动清除缓存文件时间。
#max_size:硬盘空间最大可使用值。

#指定临时缓存文件的存储路径(路径需和上面路径在同一分区)
proxy_temp_path /data/proxy/temp

#服务配置
server {
#侦听的80端口
listen 80;
server_name localhost;
location / {
#反向代理缓存设置命令(proxy_cache zone|off,默认关闭所以要设置)
proxy_cache cache_one;
#对不同的状态码缓存不同时间
proxy_cache_valid 200 304 12h;
#设置以什么样参数获取缓存文件名
proxy_cache_key $host$uri$is_args$args;
#后7端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#代理设置
proxy_pass http://IP;
#文件过期时间控制
expires 1d;
}
#配置手动清楚缓存(实现此功能需第三方模块 ngx_cache_purge)
#http://www.123.com/2017/0316/17.html访问
#http://www.123.com/purge/2017/0316/17.html清楚URL缓存
location ~ /purge(/.*) {
allow 127.0.0.1;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
#设置扩展名以.jsp、.php、.jspx结尾的动态应用程序不做缓存
location ~.*\.(jsp|php|jspx)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://http://IP;
}

​ 负载均衡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#负载均衡服务器池
upstream my_server_pool {
#调度算法
#1.轮循(默认)(weight轮循权值)
#2.ip_hash:根据每个请求访问IP的hash结果分配。(会话保持)
#3.fair:根据后端服务器响应时间最短请求。(upstream_fair模块)
#4.url_hash:根据访问的url的hash结果分配。(需hash软件包)
#参数:
#down:表示不参与负载均衡
#backup:备份服务器
#max_fails:允许最大请求错误次数
#fail_timeout:请求失败后暂停服务时间。
server 192.168.1.109:80 weight=1 max_fails=2 fail_timeout=30;
server 192.168.1.108:80 weight=2 max_fails=2 fail_timeout=30;
}
#负载均衡调用
server {
...
location / {
proxy_pass http://my_server_pool;
}
}

​ URL重写

1
2
3
4
5
6
7
8
9
10
11
#根据不同的浏览器URL重写
if($http_user_agent ~ Firefox){
rewrite ^(.*)$ /firefox/$1 break;
}
if($http_user_agent ~ MSIE){
rewrite ^(.*)$ /msie/$1 break;
}
#实现域名跳转
location / {
rewrite ^/(.*)$ https://web8.example.com$1 permanent;
}

​ IP限制

1
2
3
4
5
6
7
#限制IP访问
location / {
deny 192.168.0.2;
allow 192.168.0.0/24;
allow 192.168.1.1;
deny all;
}

3. Nginx启停

1
2
# /etc/init.d/nginx start
# /etc/init.d/nginx stop
---------------- The End ----------------