当前位置:  操作系统/服务器>linux
本页文章导读:
    ▪nginx配置gzip压缩一例      nginx的gzip压缩使用http_gzip_module模块,此模块支持在线实时压缩输出数据流。 范例: 代码如下: #nginx gzip gzip on; gzip_min_length  1000; gzip_proxied  expired no-cache no-store private auth; gzip_types   text.........
    ▪Nginx的Gzip压缩配置      gzip(GNU-ZIP)是一种压缩技术。 经gzip压缩后的页面大小可以变为原来的30%甚至更小,因此很多网站使用gzip压缩以降低网站带宽消耗,同时提升访问速度,使访问者拥有更好的用户体验。 gzip的.........
    ▪CentOS6.1部署nginx、php(包括fastcgi)、虚拟主机配置      部署时间:2012-07-24 OS环境:CentOS 6.1 nginx:nginx-1.2.2 PHP:PHP5.3.14 0、安装依赖包   代码如下: yum install openssl-devel pcre-devel zlib-devel libjpeg-devel libpng-devel freetype-devel gcc make 1、添加 www 用.........

[1]nginx配置gzip压缩一例
    来源: 互联网  发布时间: 2013-12-24

nginx的gzip压缩使用http_gzip_module模块,此模块支持在线实时压缩输出数据流。
范例:

代码如下:
#nginx gzip
gzip on;
gzip_min_length  1000;
gzip_proxied  expired no-cache no-store private auth;
gzip_types   text/plain application/x-javascript text/css text/html application/xml;

参数说明:
gzip on|off
开启或者关闭gzip模块

gzip_min_length 1000
设置允许压缩的页面最小字节数,页面字节数从header头中的Content-Length中进行获取。
默认值是0,不管页面多大都压缩。
建议设置成大于1k的字节数,小于1k可能会越压越大,即: gzip_min_length 1024。

gzip_proxied expired no-cache no-store private auth;
Nginx作为反向代理的时候启用,开启或者关闭后端服务器返回的结果,匹配的前提是后端服务器必须要返回包含”Via”的 header头。

gzip_types text/plain application/xml;
匹配MIME类型进行压缩,(无论是否指定)text/html类型总是会被压缩的。


    
[2]Nginx的Gzip压缩配置
    来源: 互联网  发布时间: 2013-12-24

gzip(GNU-ZIP)是一种压缩技术。
经gzip压缩后的页面大小可以变为原来的30%甚至更小,因此很多网站使用gzip压缩以降低网站带宽消耗,同时提升访问速度,使访问者拥有更好的用户体验。
gzip的压缩页面需要浏览器和服务器双方都支持,实际上就是服务器端压缩,传到浏览器后浏览器解压并解析。
目前绝大多数浏览器都支持解析gzip压缩过的页面。

Nginx的压缩输出由一组gzip压缩指令来实现。
相关指令位于http{….}两个大括号之间。

gzip on;
//该指令用于开启或关闭gzip模块(on/off)

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

gzip_buffers 4 16k;
//设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。
4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存。

gzip_http_version 1.1;
//识别http的协议版本(1.0/1.1)

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

gzip_types text/plain application/x-javascript text/css application/xml
//匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的。

gzip_vary on;
//和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持。
因此,为避免浪费不支持的也压缩,需要根据客户端的HTTP头来判断,是否需要压缩。

提供两段gzip压缩代码,供大家学习参考。

nginx配置gzip段1:
 

代码如下:
gzip on;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

 

nginx配置gzip段2:
代码如下:
# output compression saves bandwidth
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# make sure gzip does not lose large gzipped js or css files
# see http://blog.leetsoft.com/2007/7/25/nginx-gzip-ssl
gzip_buffers 16 8k; # Disable gzip for certain browsers.
gzip_disable “MSIE [1-6].(?!.*SV1)”;

    
[3]CentOS6.1部署nginx、php(包括fastcgi)、虚拟主机配置
    来源: 互联网  发布时间: 2013-12-24

部署时间:2012-07-24
OS环境:CentOS 6.1
nginx:nginx-1.2.2
PHP:PHP5.3.14
0、安装依赖包
 

代码如下:
yum install openssl-devel pcre-devel zlib-devel libjpeg-devel libpng-devel freetype-devel gcc make


1、添加 www 用户用来执行nginx
 

代码如下:
useradd -M -r -s /sbin/nologin -d /opt/web/ www


2、创建临时目录
 

代码如下:

mkdir -p /var/tmp/nginx/client/
mkdir -p /var/tmp/nginx/proxy/
mkdir -p /var/tmp/nginx/fcgi/
 


3、下载nginx最新稳定版源代码
 

代码如下:

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.2.2.tar.gz
 


4、解压,编译,安装
 

代码如下:

tar vxzf nginx-1.2.2.tar.gz
cd nginx-1.2.2/
./configure
--prefix=/opt/web/nginx
--error-log-path=/var/log/nginx/error.log
--pid-path=/var/run/nginx/nginx.pid
--lock-path=/var/lock/nginx.lock
--user=www
--group=www
--with-http_ssl_module
--with-http_stub_status_module
--with-http_gzip_static_module
--http-log-path=/var/log/nginx/access.log
--http-client-body-temp-path=/var/tmp/nginx/client/
--http-proxy-temp-path=/var/tmp/nginx/proxy/
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/
make
make install
 


5、配置nginx
 

代码如下:

vim /opt/web/nginx/conf/nginx.conf
# 指定启动用户:
user www www;
# 进程数量,nginx作者认为一个就可以,根据自己的访问量修改
worker_processes 1;
# 设置错误日志:
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log /var/log/nginx/error.default.log;
pid /opt/web/nginx/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
charset utf-8;
include 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 logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types text/plain text/css text/xml
application/x-javascript application/xml
application/atom+xml text/javascript;
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root 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 html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# 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;
include fastcgi.conf;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /.ht {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
proxy_read_timeout 200;
# Only retry if there was a communication error, not a timeout
# on the Tornado server (to avoid propagating "queries of death"
# to all frontends)
proxy_next_upstream error;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 引入虚拟主机文件
include /opt/web/nginx/conf/sites/*.conf;

 
6、建立虚拟机配置文件存放的目录
 
代码如下:
mkdir /opt/web/nginx/conf/sites


这样配置后,需要新增加虚拟主机的直接在 nginx/conf/sites/目录下,添加配置文件即可
例如:现在有 www. 域名
建立:/opt/web/nginx/conf/sites/www..conf 文件
内容如下:  

代码如下:

server {
listen 80;
client_max_body_size 10M;
#多个域名用空格分割,第一个为默认
server_name www. ;
charset UTF-8;
index index.html index.htm index.php;
# 定义根目录
set $root /var/webroot/www./;
# 设置站点路径
root $root;
# 防止目录浏览
autoindex off;
if ($host != 'www.') {
rewrite ^/(.*)$ http://www./$1 permanent;
}
# 防止.htaccess文件被请求
location ~ /.ht {
deny all;
}
error_page 404 /404.html;
index index.html index.htm;
location /uploads/ {
alias /data/webroot/www./uploads/;
}
try_files $uri @uwsgi;
location @uwsgi{
# 将其它的请求转交给uwsgi
include uwsgi_params;
uwsgi_pass unix:/tmp/360ito_uwsgi.sock;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_pass http://localhost:5000;
}
# 将php类型的请求转交给fastcgi
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
# 访问日志:
access_log /var/log/nginx/access.www..log;
# 加载.htaccess重写文件,注意,这里不支持变量路径
# 不能写成 include $root/www./.htaccess;
# include /var/webroot/www./.htaccess;
# 开启域名跳转,则当访问出错后,其他域名会自动跳转到 www.
# 注意,这里我说的是,仅仅当访问出错后,才会跳转,所以,这里并不能实现301重定向!
server_name_in_redirect on;

 
7、安装最新版本PHP( PHP5.3.14 )
 
代码如下:

cd /usr/local/src/
wget http://cn.php.net/get/php-5.3.14.tar.bz2/from/this/mirror
tar xjvf php-5.3.14.tar.bz2
cd php-5.3.14
 
执行:
 
代码如下:
./buildconf --force

如果报错,可能是你的 autoconf不是 2.13 版本的,PHP5.3.系列的bug,需要安装 autoconf为2.13的版本:
 

代码如下:

CentOS : # yum install autoconf213
Debian : # apt-get install autoconf2.13
 设置环境变量
 
代码如下:

# CentOS :
export PHP_AUTOCONF="/usr/bin/autoconf-2.13"
# Debian :
export PHP_AUTOCONF="/usr/bin/autoconf2.13"
 再次运行:./buildconf --force ,出现 buildconf: autoconf version 2.13 (ok)
,则表示成功。
编译安装 PHP
 
代码如下:

./configure
--prefix=/opt/web/php
--with-config-file-path=/opt/web/php/etc
--with-config-file-scan-dir=/opt/web/php/etc/conf.d
--enable-fpm
--with-fpm-user=www
--with-fpm-group=www
--with-mysql=/opt/db/Percona-Server-5.5.14-rel20.5
--with-mysqli=/opt/db/Percona-Server-5.5.14-rel20.5/bin/mysql_config
--with-iconv-dir
--with-freetype-dir
--with-jpeg-dir
--with-png-dir
--with-zlib
--with-libxml-dir
--enable-xml
--enable-mbstring
--with-gd
--enable-gd-native-ttf
--with-openssl
--enable-inline-optimization
make && make install
cp php.ini-production /opt/web/php/etc/php.ini
cd /opt/web/php/etc
cp php-fpm.conf.default php-fpm.conf
 修改php-fpm.conf 启用如下几行,即去掉前面的分号(;)
 
代码如下:

pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = notice
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
 8、启动php-fpm
 
代码如下:
/opt/web/php/sbin/php-fpm


启动nginx
 

代码如下:
/opt/web/nginx/sbin/nginx


9、测试一下
 

代码如下:
vim /var/webroot/www./test.php


输入和保存
 

代码如下:

<?PHP
phpinfo();
?>
 10、在浏览器地址栏输入:http://test./test.php
成功的话,可以看到phpinfo()输出的信息

    
最新技术文章:
▪linux系统中的列出敏感用户的脚本代码
▪a10 config backup for aXAPI
▪一键备份gitolite服务器的Shell脚本
▪nagios 分发文件实现代码
▪阿里云云服务器Linux系统更新yum源Shell脚本
▪一个监控LINUX目录和文件变化的Shell脚本分享
▪Linux下实现SSH免密码登录和实现秘钥的管理、...
▪Shell正则表达式之grep、sed、awk实操笔记
▪3个备份系统文件并邮件发送的Shell脚本分享
▪CentOS 6.3下给PHP添加mssql扩展模块教程
▪监控网站是否可以正常打开的Shell脚本分享
▪shell脚本编程之if语句学习笔记
▪shell脚本编程之循环语句学习笔记
▪shell脚本编程之case语句学习笔记
▪Shell脚本实现的阳历转农历代码分享
▪Shell脚本实现复制文件到多台服务器的代码分...
▪Shell脚本实现批量下载网络图片代码分享
▪Shell脚本实现检测文件是否被修改过代码分享
▪Shell脚本数组用法小结
▪Shell脚本批量重命名文件后缀的3种实现
▪C语言实现的ls命令源码分享
▪Linux下查找后门程序 CentOS 查后门程序的shell脚...
▪Shell 函数参数
▪linux shell 自定义函数方法(定义、返回值、变...
▪Shell实现判断进程是否存在并重新启动脚本分...
▪Shell脚本break和continue命令简明教程
▪Shell脚本函数定义和函数参数
▪让代码整洁、过程清晰的BASH Shell编程技巧
▪shell常用重定向实例讲解
▪awk中RS、ORS、FS、OFS的区别和联系小结
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3