当前位置: > Linux服务器 > 环境配置 >

腾讯云环境配置之Nginx1.6.2稳定版

时间:2015-01-18 01:39来源:linux.it.net.cn 作者:IT

配置nginx + php + redis 。这三个软件我都准备装最新的稳定版.
本篇记录一下安装nginx1.6.2
服务器环境:centos6.3 x86_64

 
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
#下载nginx并且解压缩
cd ~
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/src/
cd /usr/local/src
#yum update
#yum makecache
#yum groupinstall development tools
#yum pcre-devel zlib-devel openssl-devel
#创建用户和组
groupadd -r nginx
useradd -r -g nginx -s /sbin/nologin -M nginx
#创建web根目录和nginx日志文件并且授予权限
mkdir -p /data/wwwroot
mkdir -p /var/log/nginx
chmod +w /data/wwwroot/
chmod +w /var/log/nginx/
chown -R nginx:nginx /data/wwwroot/
chown -R nginx:nginx /var/log/nginx/
#配置nginx
cd /usr/local/src/nginx-1.6.2/
./configure --user=nginx --group=nginx \
> --prefix=/usr/local/nginx \
> --sbin-path=/usr/local/nginx/sbin/nginx \
> --conf-path=/usr/local/nginx/etc/conf/nginx.conf \
> --pid-path=/var/run/nginx/nginx.pid \
> --lock-path=/var/lock/subsys/nginx.lock \
> --error-log-path=/var/log/nginx/error.log \
> --http-log-path=/var/longinx/access.log \
> --with-http_stub_status_module \
> --with-http_ssl_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_realip_module \
> --with-file-aio
make
make install
#做一个软链接,方便使用nginx命令
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
#配置nginx启动脚本
http://wiki.nginx.org/RedHatNginxInitScript
#拷贝链接中的启动脚本修改如下参数
nginx 和 NGINX_CONF_FILE对应的路径
并且把修改好的脚本写到/etc/init.d/nginx中
chmod +x /etc/init.d/nginx
#加入到开机启动
chkconfig --add nginx
chkconfig nginx on
#使用nginx管理脚本启动nginx
/etc/init.d/nginx start

下面讲一下我的nginx配置
首先借鉴C语言函数编程的模式,我们可以把nginx的配置项拆分为若干个文件。用到什么模块可以使用include把相应的配置文件载入过来。具体看操作
创建一个php模块

 
1
2
3
4
5
6
7
8
#创建模块文件
vi /usr/local/nginx/etc/conf/include_php.conf
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include        fastcgi_params;
fastcgi_param SERVER_NAME $host;
fastcgi_intercept_errors on;

创建一个静态资源缓存模块

 
1
2
3
4
5
6
7
8
9
10
#配置静态资源缓存
vi /usr/local/nginx/etc/conf/expire.conf
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
        expires      30d;
}
location ~ .*\.(js|css)?$
{
        expires      1h;
}

配置wordpress伪静态和url重写模块

 
1
2
3
4
5
6
rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last;
    if (!-e $request_filename) {
           rewrite ^.+?(/wp-.*) $1 last;
           rewrite ^.+?(/.*\.php)$ $1 last;
           rewrite ^ /index.php last;
    }

等等。这里就距几个例子
下面配置我们公共使用的模块

 
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
vi /usr/local/nginx/etc/conf/nginx.conf
#写入如下内容
user  nginx nginx;
worker_processes  2;
 
events {
        use epoll;
         worker_connections  1024;
}
 
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 20m;
 
        #性能优化
        sendfile        on;
        tcp_nopush     on;
        keepalive_timeout 60;
        tcp_nodelay on;
 
        #gzip
        gzip on;
        gzip_min_length 1k;
        gzip_buffers 4 16k;
        gzip_comp_level 6;
        gzip_types text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
 
        #FastCGI通用配置
        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 128k;
        #默认配置
        server {
                listen 80 default;
                root /dev/null;
                location / {
                        return 404;
                        access_log off;
                }
        }
        include vhost/*.conf;
}

#创建我们的基于域名的主机配置文件目录

 
1
mkdir -p /usr/local/nginx/etc/conf/vhost

拿我的博客做演示

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#以博客为例
vi /usr/local/nginx/etc/conf/vhost/blog_zhuzhenyu_net.conf
server {
    listen       80;
    server_name  blog.zhuzhenyu.net;
    root /data/wwwroot/zhuzhenyu/blog;
    index index.php index.html index.htm;
    include expire.conf;
 
    rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last;
        if (!-e $request_filename) {
                rewrite ^.+?(/wp-.*) $1 last;
                rewrite ^.+?(/.*\.php)$ $1 last;
                rewrite ^ /index.php last;
        }
 
    location ~ \.php$ {
        include include_php.conf;
    }
 
}

总结:模块化配置nginx很好的管理nginx配置文件,通用性比较强,容易管理.

(责任编辑:IT)
------分隔线----------------------------