- 浏览: 143747 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
august_000:
很有道理,我已经亲自测试过了:
public class ...
单例模式之线程安全解析 -
Chris_bing:
一个单例有这么多名堂,最后那个内部类的解决方案很有创意啊,受教 ...
单例模式之线程安全解析
原文链接:http://saiyaren.iteye.com/blog/1943626
1. Nginx无法启动解决方法
在查看到logs中报了如下错误时:
0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
是因为80端口出现了冲突
2. Weight 和ip_hash
在负载均衡均衡模块中upstream的
Weight是可以把请求的链接优先访问该服务
Ip_hash会去将客户端转发到一个可用的服务器上
如果服务器不可用需要用down来标注
还有就是weight和ip_hash不能同时使用
3. Nginx tomcat负载均衡和多域名同端口转发
Nginx进行http负载均衡的模块是upstream
Upstream可以进行多个配置,这样的话可以灵活的配置站点,但是注意的是upstream后面的名字最好是配置成为域名,因为upstream是进行http访问的,一般的解析没有问题,但是如果是ajax的解析就会通过访问upstream后面的名字来进行访问了,这里要注意。
修改配置文件:conf/nginx.conf
Upstream的server的配置:
§ weight = NUMBER - 设置服务器权重,默认为1。
§ max_fails = NUMBER - 在一定时间内(这个时间在fail_timeout参数中设置)检查这个服务器是否可用时产生的最多失败请求数,默认为1,将其设置为0可以关闭检查,这些错误在proxy_next_upstream或fastcgi_next_upstream(404错误不会使max_fails增加)中定义。
§ fail_timeout = TIME - 在这个时间内产生了max_fails所设置大小的失败尝试连接请求后这个服务器可能不可用,同样它指定了服务器不可用的时间(在下一次尝试连接请求发起之前),默认为10秒,fail_timeout与前端响应时间没有直接关系,不过可以使用proxy_connect_timeout和proxy_read_timeout来控制。
§ down - 标记服务器处于离线状态,通常和ip_hash一起使用。
§ backup - (0.6.7或更高)如果所有的非备份服务器都宕机或繁忙,则使用本服务器(无法和ip_hash指令搭配使用)。
#需要进行负载均衡的站点
#其中server是其中负载均衡的一个节点www.aaa.com
upstream www.aaa.com {
server 192.168.0.1:8080 weight=1;
server 192.168.0.2:8080 weight=2;
server 192.168.0.1:8081 weight=3;
}
#第二个网站的www.bbb.com的负载均衡的节点
upstream www.bbb.com {
server 192.168.1.1:8080 ;
server 192.168.1.2:8080 ;
server 192.168.1.3:8080 ;
ip_hash;
}
#同一服务器转发2个不同域名进行负载均衡
#www.aaa.com的server
server
{
listen 80;
server_name www.aaa.com;
location / {
index index.html index.jsp;
#这里的proxy_pass转发的是upstream的名字www.aaa.com
proxy_pass http://www.aaa.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
#limit_conn crawler 20;
}
server
{
listen 80;
server_name www.bbb.com;
location / {
index index.html index.jsp;
#这里的proxy_pass转发的是upstream的名字www.bbb.com
proxy_pass http://www.bbb.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
#limit_conn crawler 20;
}
4. linux安装nginx
先决条件
yum install gcc
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel
释放文件
tar –zxvf nginx.tar.gz
安装
./ configure –prefix=路径
make
make install
3)管理nginx服务
启动:
/usr/local/nginx/sbin/nginx
停止
/usr/local/nginx/sbin/nginx -s stop
重启
/usr/local/nginx/sbin/nginx -s reload
查看状态
netstat -autlp| grep nginx
./configure "--prefix=/export/servers/nginx" "--sbin-path=/export/servers/nginx/sbin/nginx" "--conf-path=/export/servers/nginx/conf/nginx.conf" "--error-log-path=/export/servers/nginx/logs/error.log" "--http-log-path=/export/servers/nginx/logs/access.log" "--pid-path=/export/servers/nginx/var/nginx.pid" "--lock-path=/export/servers/nginx/var/nginx.lock" "--http-client-body-temp-path=/dev/shm//nginx_temp/client_body" "--http-proxy-temp-path=/dev/shm/nginx_temp/proxy" "--http-fastcgi-temp-path=/dev/shm/nginx_temp/fastcgi" "--user=www" "--group=www" "--with-cpu-opt=pentium4F" "--without-select_module" "--without-poll_module" "--with-http_realip_module" "--with-http_sub_module" "--with-http_gzip_static_module" "--with-http_stub_status_module" "--without-http_ssi_module" "--without-http_userid_module" "--without-http_geo_module" "--without-http_map_module" "--without-mail_pop3_module" "--without-mail_imap_module" "--without-mail_smtp_module"
5. 日志输出相应时间
"$request_time"';
在log_format 后面添加上上面的参数即可
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
6. 设置cache-control
Http协议的cache-control的常见取值及其组合释义:
no-cache: 数据内容不能被缓存, 每次请求都重新访问服务器, 若有max-age, 则缓存期间不访问服务器.
no-store: 不仅不能缓存, 连暂存也不可以(即: 临时文件夹中不能暂存该资源)
private(默认): 只能在浏览器中缓存, 只有在第一次请求的时候才访问服务器, 若有max-age, 则缓存期间不访问服务器.
public: 可以被任何缓存区缓存, 如: 浏览器、服务器、代理服务器等
max-age: 相对过期时间, 即以秒为单位的缓存时间.
no-cache, private: 打开新窗口时候重新访问服务器, 若设置max-age, 则缓存期间不访问服务器.
private, 正数的max-age: 后退时候不会访问服务器
no-cache, 正数的max-age: 后退时会访问服务器
点击刷新: 无论如何都会访问服务器.
Expires:
设置以分钟为单位的绝对过期时间, 优先级比Cache-Control低, 同时设置Expires和Cache-Control则后者生效.
Last-Modified:
该资源的最后修改时间, 在浏览器下一次请求资源时, 浏览器将先发送一个请求到服务器上, 并附上If-Unmodified-Since头来说明浏览器所缓存资源的最后修改时间, 如果服务器发现没有修改, 则直接返回304(Not Modified)回应信息给浏览器(内容很少), 如果服务器对比时间发现修改了, 则照常返回所请求的资源.
在网页中设置:
<meta http-equiv="Cache-Control" content="max-age=7200" />
或
<meta http-equiv="Expires" content="Mon, 20 Jul 2009 23:00:00 GMT" />
只对本网页有效
Nginx 设置:
# 相关页面设置Cache-Control头信息
if ($request_uri ~* "^/$|^/search/.+/|^/company/.+/") {
add_header Cache-Control max-age=3600;
}
if ($request_uri ~* "^/search-suggest/|^/categories/") {
add_header Cache-Control max-age=86400;
}
全局的就在location /下面配置即可
7. 静态压缩和动态压缩的区别
静态压缩:
静态压缩是之间就通过工具将文件通过压缩工具进行压缩,然后nginx只是做文件头设置即可;如:
文件为1.html,然后压缩后为1.html.gz
然后nginx的配置文件中配置:
location ~ \.gz$ {add_header Content-Encoding gzip;
gzip off;//这里的off是不进行动态压缩
}
因为之前我们的1.html已经压缩为1.html.gz了,所以这时我们只需要设置header为gzip即可,不开启gzip动态压缩;
动态压缩:
动态压缩就是我们的文件之前不通过工具压缩,而通过nginx进行压缩,这样的为动态压缩,如:
#启动预压缩功能,对所有类型的文件都有效
gzip_static on;
#找不到预压缩文件,进行动态压缩
gzip on;
gzip_min_length 1000;
gzip_buffers 4 16k;
gzip_comp_level 5;
gzip_types text/plain application/x-javascript text/css application/xml;
#gzip公共配置
gzip_http_version 1.1
gzip_proxied expired no-cache no-store private auth;
#纠结的配置
#对于ie有个bug,响应vary头后将不会缓存请求,每次都会重新发新的请求。所以,对于ie 1-6直接禁用gzip。
gzip_disable "MSIE [1-6]\.";
#开启Http Vary头,vary头主要提供给代理服务器使用,根据Vary头做不同的处理。例如,对于支持gzip的请求反向代理缓存服务器将返回gzip内容,不支持gzip的客户端返回原始内容。
gzip_vary on;
1. gzip_static配置优先级高于gzip
2. 开启nginx_static后,对于任何文件都会先查找是否有对应的gz文件
3. gzip_types设置对gzip_static无效
8. nginx: [emerg] unknown directive "if(
nginx: [emerg] unknown directive "if($args" in /export/servers/nginx/conf/nginx.conf:90
这个错误是因为缺少 pcre 8.2的包
9. 安装nginx pcre问题解决方法
首先安装pcre需要安装openssl
然后pcre需要指定路径
-with-pcre=/usr/local/include/pcre
注意后面不要加斜杠
首先,
mkdir /usr/local/include/pcre
mkdir /usr/local/include/pcre/.libs
然后把pcre的包cp到指定路径下:
cp /opt/pcre/lib/libpcre.a /usr/local/include/pcre/libpcre.a
cp /opt/pcre/lib/libpcre.a /usr/local/include/pcre/libpcre.la
cp /opt/pcre/include/pcre.h /usr/local/include/pcre/pcre.h
cp /usr/local/include/pcre/*.* /usr/local/include/pcre/.libs
/opt/pcre是pcre的安装路径这里可自定,cp完文件后,我们将nginx配置安装
./configure --prefix=/opt/nginx --with-pcre=/usr/local/include/pcre
注意:
--with-pcre=/usr/local/include/pcre
这里必须是/usr/local/include/pcre路径,安装路径不行,这里尝试过了,否则失败后很头疼;
第二当编译好后,报如下错误:
make -f objs/Makefile
make[1]: Entering directory `/export/software/nginx/jdws-1.0'
cd /usr/local/include/pcre \
&& if [ -f Makefile ]; then make distclean; fi \
&& CC="gcc" CFLAGS="" \
./configure --disable-shared
/bin/sh: line 2: ./configure: ûÓÐÄǸöÎļþ»òĿ¼
make[1]: *** [/usr/local/include/pcre/Makefile] ´íÎó 127
make[1]: Leaving directory `/export/software/nginx/jdws-1.0'
make: *** [build] ´íÎó 2
我们修改nginx安装目录下的objs/MakeFile文件中的
/usr/local/include/pcre/Makefile: objs/Makefile
cd /usr/local/include/pcre \
&& if [ -f Makefile ]; then $(MAKE) distclean; fi \
&& CC="$(CC)" CFLAGS="" \
./configure --disable-shared
大约在994行,删除./configure --disable-shared这行内容后,然后make 和make install就没有问题了
10. Nginx反向代理获取真实IP
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
11. nginx 使用if错误
当出现如下错误是:
[emerg]: unknown directive "if($request_method" in /export/servers/nginx/conf/nginx.conf:86
一种是少pcre包
另一种是:
if 和括号间需要空格,坑爹啊
12. 设定cache头
add_header X-Cache '$upstream_cache_status from $server_addr';
13. nginx cache状态缓存
proxy_cache_valid 200 304 20m;
如果要缓存内容,需要加上如上内容,这是对http状态值进行缓存,并且设定缓存时间。
14. $request_uri
$request_uri获取当前浏览器完整路径
$uri$is_args$args 这个是获取真实请求路径
15. Rewrite需要写在location
用rewrite报404
后来查看到配置中rewrite 在server内location外,这样是有问题的,把rewrite放到location中就没问题了
16. 调试nginx
修改config并编译
由于gdb需要gcc的时候加上-g参数,这样生成的文件才能使用gdb调试,因此我们要对源码做一下小改动
修改auto/cc/conf文件
ngx_compile_opt="-c"
变为
ngx_compile_opt="-c -g"
执行configure
./configure --prefix=/home/yejianfeng/nginx/
确认
发现多出了objs文件夹,里面有Makefile文件
确认一下-g参数是否加上了
vim objs/Makefile
17. Nginx 高并发设置
并发100时,nginx+php 可以到850,但是300-500并发却低到400-500,这不正常,所以需要对linux系统的环境进行设置
================================================================
vi /etc/sysctl.conf CentOS5.5中可以将所有内容清空直接替换为如下内容:
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
使配置立即生效可使用如下命令:
sysctl -p
==============================================================
在/etc/security/limits.conf最后增加:
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
具体使用哪种,在 CentOS 中使用第1 种方式无效果,使用第3 种方式有效果,而在Debian 中使用第2 种有效果
参考:
http://hi.baidu.com/touchiyudeji/item/a699730b80ba78d9dde5b00e
18. nginx 日志格式配置
log_format main '$remote_addr - $remote_user [$time_local] ' '-----"$request"------- $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"' '"addr:$upstream_addr - status:$upstream_status - cachestatus:$upstream_cache_status"'
'- cacheKey:"$host:$server_port$request_uri"' ;
请求的upstream 的地址$upstream_addr
请求upstream 的状态$upstream_status
请求的upstream 的cache的状态$upstream_cache_status
19. Nginx if 判断条件中的分组()不能超过9个,超过9个再非rewrite的会出现aborted问题
if ($request ~* .*/((1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9))){
}
这种情况下,非rewrite的内容将显示aborted问题,解决方式由多个if进行判断
20. error_page 切换到指定的location
定义一个location ,用@定义一个名称是外面无法访问的,一般用在try_files和error_page中
location @hhvm_error_to_php {
include fastcgi_params;
# fastcgi_pass unix:/dev/shm/php-fcgi.sock;
fastcgi_pass php_servers;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /export/data/www/comm.360buy.com_test$fastcgi_script_name;
fastcgi_connect_timeout 3;
fastcgi_send_timeout 5;
fastcgi_read_timeout 5;
}
location ~ \.php$ {
#################hhvm###############################################################################
#edit date:20130724
#edit author:huzhiguang
#function: access url to hhvm
###################################################################################################
if ($request ~* .*/(ProductPageService\.aspx|clubservice\.aspx|(productpage/p-(\d*)-s-(\d*)-t-(\d*)-p-(\d*)\.html.*)|(clubservice/newcomment-(.*)-(\d*)\.html.*))){
#当遇到错误时500 502 503 504时跳转到hhvm_error_to_php这个location由php处理
error_page 500 502 503 504 = @hhvm_error_to_php ;
proxy_pass http://hhvms;
#当if匹配中使用了break ,则下面不会再继续进行匹配,那么也就不用去判断非了
break;
}
#加上改行后,proxy返回500后,会拦截进行error_page处理,默认不处理
proxy_intercept_errors on;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
##################################################################################################
include fastcgi_params;
# fastcgi_pass unix:/dev/shm/php-fcgi.sock;
fastcgi_pass php_servers;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /export/data/www/comm.360buy.com_test$fastcgi_script_name;
fastcgi_connect_timeout 3;
fastcgi_send_timeout 5;
fastcgi_read_timeout 5;
}
如果加上该行,php如果出现了500,nginx会进行error_page处理,默认不处理
fastcgi_intercept_errors on;
21. Nginx遇到访问aborted解决
当nginx访问所有的aborted都不行时,配置又没有问题,reload也不生效时,然后将所有的nginx进行kill掉,然后重启就好了,
这个是我发现因为我更新了一个动态链接库:
libz.so.1 => /export/servers/hhvm-1.1/support_lib/libz.so.1 (0x00007fd912710000)
然后影响了nginx的运行,所以造成了这种reload无效的问题
1. Nginx无法启动解决方法
在查看到logs中报了如下错误时:
0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
是因为80端口出现了冲突
2. Weight 和ip_hash
在负载均衡均衡模块中upstream的
Weight是可以把请求的链接优先访问该服务
Ip_hash会去将客户端转发到一个可用的服务器上
如果服务器不可用需要用down来标注
还有就是weight和ip_hash不能同时使用
3. Nginx tomcat负载均衡和多域名同端口转发
Nginx进行http负载均衡的模块是upstream
Upstream可以进行多个配置,这样的话可以灵活的配置站点,但是注意的是upstream后面的名字最好是配置成为域名,因为upstream是进行http访问的,一般的解析没有问题,但是如果是ajax的解析就会通过访问upstream后面的名字来进行访问了,这里要注意。
修改配置文件:conf/nginx.conf
Upstream的server的配置:
§ weight = NUMBER - 设置服务器权重,默认为1。
§ max_fails = NUMBER - 在一定时间内(这个时间在fail_timeout参数中设置)检查这个服务器是否可用时产生的最多失败请求数,默认为1,将其设置为0可以关闭检查,这些错误在proxy_next_upstream或fastcgi_next_upstream(404错误不会使max_fails增加)中定义。
§ fail_timeout = TIME - 在这个时间内产生了max_fails所设置大小的失败尝试连接请求后这个服务器可能不可用,同样它指定了服务器不可用的时间(在下一次尝试连接请求发起之前),默认为10秒,fail_timeout与前端响应时间没有直接关系,不过可以使用proxy_connect_timeout和proxy_read_timeout来控制。
§ down - 标记服务器处于离线状态,通常和ip_hash一起使用。
§ backup - (0.6.7或更高)如果所有的非备份服务器都宕机或繁忙,则使用本服务器(无法和ip_hash指令搭配使用)。
#需要进行负载均衡的站点
#其中server是其中负载均衡的一个节点www.aaa.com
upstream www.aaa.com {
server 192.168.0.1:8080 weight=1;
server 192.168.0.2:8080 weight=2;
server 192.168.0.1:8081 weight=3;
}
#第二个网站的www.bbb.com的负载均衡的节点
upstream www.bbb.com {
server 192.168.1.1:8080 ;
server 192.168.1.2:8080 ;
server 192.168.1.3:8080 ;
ip_hash;
}
#同一服务器转发2个不同域名进行负载均衡
#www.aaa.com的server
server
{
listen 80;
server_name www.aaa.com;
location / {
index index.html index.jsp;
#这里的proxy_pass转发的是upstream的名字www.aaa.com
proxy_pass http://www.aaa.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
#limit_conn crawler 20;
}
server
{
listen 80;
server_name www.bbb.com;
location / {
index index.html index.jsp;
#这里的proxy_pass转发的是upstream的名字www.bbb.com
proxy_pass http://www.bbb.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
#limit_conn crawler 20;
}
4. linux安装nginx
先决条件
yum install gcc
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel
释放文件
tar –zxvf nginx.tar.gz
安装
./ configure –prefix=路径
make
make install
3)管理nginx服务
启动:
/usr/local/nginx/sbin/nginx
停止
/usr/local/nginx/sbin/nginx -s stop
重启
/usr/local/nginx/sbin/nginx -s reload
查看状态
netstat -autlp| grep nginx
./configure "--prefix=/export/servers/nginx" "--sbin-path=/export/servers/nginx/sbin/nginx" "--conf-path=/export/servers/nginx/conf/nginx.conf" "--error-log-path=/export/servers/nginx/logs/error.log" "--http-log-path=/export/servers/nginx/logs/access.log" "--pid-path=/export/servers/nginx/var/nginx.pid" "--lock-path=/export/servers/nginx/var/nginx.lock" "--http-client-body-temp-path=/dev/shm//nginx_temp/client_body" "--http-proxy-temp-path=/dev/shm/nginx_temp/proxy" "--http-fastcgi-temp-path=/dev/shm/nginx_temp/fastcgi" "--user=www" "--group=www" "--with-cpu-opt=pentium4F" "--without-select_module" "--without-poll_module" "--with-http_realip_module" "--with-http_sub_module" "--with-http_gzip_static_module" "--with-http_stub_status_module" "--without-http_ssi_module" "--without-http_userid_module" "--without-http_geo_module" "--without-http_map_module" "--without-mail_pop3_module" "--without-mail_imap_module" "--without-mail_smtp_module"
5. 日志输出相应时间
"$request_time"';
在log_format 后面添加上上面的参数即可
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
6. 设置cache-control
Http协议的cache-control的常见取值及其组合释义:
no-cache: 数据内容不能被缓存, 每次请求都重新访问服务器, 若有max-age, 则缓存期间不访问服务器.
no-store: 不仅不能缓存, 连暂存也不可以(即: 临时文件夹中不能暂存该资源)
private(默认): 只能在浏览器中缓存, 只有在第一次请求的时候才访问服务器, 若有max-age, 则缓存期间不访问服务器.
public: 可以被任何缓存区缓存, 如: 浏览器、服务器、代理服务器等
max-age: 相对过期时间, 即以秒为单位的缓存时间.
no-cache, private: 打开新窗口时候重新访问服务器, 若设置max-age, 则缓存期间不访问服务器.
private, 正数的max-age: 后退时候不会访问服务器
no-cache, 正数的max-age: 后退时会访问服务器
点击刷新: 无论如何都会访问服务器.
Expires:
设置以分钟为单位的绝对过期时间, 优先级比Cache-Control低, 同时设置Expires和Cache-Control则后者生效.
Last-Modified:
该资源的最后修改时间, 在浏览器下一次请求资源时, 浏览器将先发送一个请求到服务器上, 并附上If-Unmodified-Since头来说明浏览器所缓存资源的最后修改时间, 如果服务器发现没有修改, 则直接返回304(Not Modified)回应信息给浏览器(内容很少), 如果服务器对比时间发现修改了, 则照常返回所请求的资源.
在网页中设置:
<meta http-equiv="Cache-Control" content="max-age=7200" />
或
<meta http-equiv="Expires" content="Mon, 20 Jul 2009 23:00:00 GMT" />
只对本网页有效
Nginx 设置:
# 相关页面设置Cache-Control头信息
if ($request_uri ~* "^/$|^/search/.+/|^/company/.+/") {
add_header Cache-Control max-age=3600;
}
if ($request_uri ~* "^/search-suggest/|^/categories/") {
add_header Cache-Control max-age=86400;
}
全局的就在location /下面配置即可
7. 静态压缩和动态压缩的区别
静态压缩:
静态压缩是之间就通过工具将文件通过压缩工具进行压缩,然后nginx只是做文件头设置即可;如:
文件为1.html,然后压缩后为1.html.gz
然后nginx的配置文件中配置:
location ~ \.gz$ {add_header Content-Encoding gzip;
gzip off;//这里的off是不进行动态压缩
}
因为之前我们的1.html已经压缩为1.html.gz了,所以这时我们只需要设置header为gzip即可,不开启gzip动态压缩;
动态压缩:
动态压缩就是我们的文件之前不通过工具压缩,而通过nginx进行压缩,这样的为动态压缩,如:
#启动预压缩功能,对所有类型的文件都有效
gzip_static on;
#找不到预压缩文件,进行动态压缩
gzip on;
gzip_min_length 1000;
gzip_buffers 4 16k;
gzip_comp_level 5;
gzip_types text/plain application/x-javascript text/css application/xml;
#gzip公共配置
gzip_http_version 1.1
gzip_proxied expired no-cache no-store private auth;
#纠结的配置
#对于ie有个bug,响应vary头后将不会缓存请求,每次都会重新发新的请求。所以,对于ie 1-6直接禁用gzip。
gzip_disable "MSIE [1-6]\.";
#开启Http Vary头,vary头主要提供给代理服务器使用,根据Vary头做不同的处理。例如,对于支持gzip的请求反向代理缓存服务器将返回gzip内容,不支持gzip的客户端返回原始内容。
gzip_vary on;
1. gzip_static配置优先级高于gzip
2. 开启nginx_static后,对于任何文件都会先查找是否有对应的gz文件
3. gzip_types设置对gzip_static无效
8. nginx: [emerg] unknown directive "if(
nginx: [emerg] unknown directive "if($args" in /export/servers/nginx/conf/nginx.conf:90
这个错误是因为缺少 pcre 8.2的包
9. 安装nginx pcre问题解决方法
首先安装pcre需要安装openssl
然后pcre需要指定路径
-with-pcre=/usr/local/include/pcre
注意后面不要加斜杠
首先,
mkdir /usr/local/include/pcre
mkdir /usr/local/include/pcre/.libs
然后把pcre的包cp到指定路径下:
cp /opt/pcre/lib/libpcre.a /usr/local/include/pcre/libpcre.a
cp /opt/pcre/lib/libpcre.a /usr/local/include/pcre/libpcre.la
cp /opt/pcre/include/pcre.h /usr/local/include/pcre/pcre.h
cp /usr/local/include/pcre/*.* /usr/local/include/pcre/.libs
/opt/pcre是pcre的安装路径这里可自定,cp完文件后,我们将nginx配置安装
./configure --prefix=/opt/nginx --with-pcre=/usr/local/include/pcre
注意:
--with-pcre=/usr/local/include/pcre
这里必须是/usr/local/include/pcre路径,安装路径不行,这里尝试过了,否则失败后很头疼;
第二当编译好后,报如下错误:
make -f objs/Makefile
make[1]: Entering directory `/export/software/nginx/jdws-1.0'
cd /usr/local/include/pcre \
&& if [ -f Makefile ]; then make distclean; fi \
&& CC="gcc" CFLAGS="" \
./configure --disable-shared
/bin/sh: line 2: ./configure: ûÓÐÄǸöÎļþ»òĿ¼
make[1]: *** [/usr/local/include/pcre/Makefile] ´íÎó 127
make[1]: Leaving directory `/export/software/nginx/jdws-1.0'
make: *** [build] ´íÎó 2
我们修改nginx安装目录下的objs/MakeFile文件中的
/usr/local/include/pcre/Makefile: objs/Makefile
cd /usr/local/include/pcre \
&& if [ -f Makefile ]; then $(MAKE) distclean; fi \
&& CC="$(CC)" CFLAGS="" \
./configure --disable-shared
大约在994行,删除./configure --disable-shared这行内容后,然后make 和make install就没有问题了
10. Nginx反向代理获取真实IP
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
11. nginx 使用if错误
当出现如下错误是:
[emerg]: unknown directive "if($request_method" in /export/servers/nginx/conf/nginx.conf:86
一种是少pcre包
另一种是:
if 和括号间需要空格,坑爹啊
12. 设定cache头
add_header X-Cache '$upstream_cache_status from $server_addr';
13. nginx cache状态缓存
proxy_cache_valid 200 304 20m;
如果要缓存内容,需要加上如上内容,这是对http状态值进行缓存,并且设定缓存时间。
14. $request_uri
$request_uri获取当前浏览器完整路径
$uri$is_args$args 这个是获取真实请求路径
15. Rewrite需要写在location
用rewrite报404
后来查看到配置中rewrite 在server内location外,这样是有问题的,把rewrite放到location中就没问题了
16. 调试nginx
修改config并编译
由于gdb需要gcc的时候加上-g参数,这样生成的文件才能使用gdb调试,因此我们要对源码做一下小改动
修改auto/cc/conf文件
ngx_compile_opt="-c"
变为
ngx_compile_opt="-c -g"
执行configure
./configure --prefix=/home/yejianfeng/nginx/
确认
发现多出了objs文件夹,里面有Makefile文件
确认一下-g参数是否加上了
vim objs/Makefile
17. Nginx 高并发设置
并发100时,nginx+php 可以到850,但是300-500并发却低到400-500,这不正常,所以需要对linux系统的环境进行设置
================================================================
vi /etc/sysctl.conf CentOS5.5中可以将所有内容清空直接替换为如下内容:
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.tcp_max_tw_buckets = 6000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 16384 4194304
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_keepalive_time = 30
net.ipv4.ip_local_port_range = 1024 65000
使配置立即生效可使用如下命令:
sysctl -p
==============================================================
在/etc/security/limits.conf最后增加:
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
具体使用哪种,在 CentOS 中使用第1 种方式无效果,使用第3 种方式有效果,而在Debian 中使用第2 种有效果
参考:
http://hi.baidu.com/touchiyudeji/item/a699730b80ba78d9dde5b00e
18. nginx 日志格式配置
log_format main '$remote_addr - $remote_user [$time_local] ' '-----"$request"------- $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"' '"addr:$upstream_addr - status:$upstream_status - cachestatus:$upstream_cache_status"'
'- cacheKey:"$host:$server_port$request_uri"' ;
请求的upstream 的地址$upstream_addr
请求upstream 的状态$upstream_status
请求的upstream 的cache的状态$upstream_cache_status
19. Nginx if 判断条件中的分组()不能超过9个,超过9个再非rewrite的会出现aborted问题
if ($request ~* .*/((1)|(2)|(3)|(4)|(5)|(6)|(7)|(8)|(9))){
}
这种情况下,非rewrite的内容将显示aborted问题,解决方式由多个if进行判断
20. error_page 切换到指定的location
定义一个location ,用@定义一个名称是外面无法访问的,一般用在try_files和error_page中
location @hhvm_error_to_php {
include fastcgi_params;
# fastcgi_pass unix:/dev/shm/php-fcgi.sock;
fastcgi_pass php_servers;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /export/data/www/comm.360buy.com_test$fastcgi_script_name;
fastcgi_connect_timeout 3;
fastcgi_send_timeout 5;
fastcgi_read_timeout 5;
}
location ~ \.php$ {
#################hhvm###############################################################################
#edit date:20130724
#edit author:huzhiguang
#function: access url to hhvm
###################################################################################################
if ($request ~* .*/(ProductPageService\.aspx|clubservice\.aspx|(productpage/p-(\d*)-s-(\d*)-t-(\d*)-p-(\d*)\.html.*)|(clubservice/newcomment-(.*)-(\d*)\.html.*))){
#当遇到错误时500 502 503 504时跳转到hhvm_error_to_php这个location由php处理
error_page 500 502 503 504 = @hhvm_error_to_php ;
proxy_pass http://hhvms;
#当if匹配中使用了break ,则下面不会再继续进行匹配,那么也就不用去判断非了
break;
}
#加上改行后,proxy返回500后,会拦截进行error_page处理,默认不处理
proxy_intercept_errors on;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
##################################################################################################
include fastcgi_params;
# fastcgi_pass unix:/dev/shm/php-fcgi.sock;
fastcgi_pass php_servers;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /export/data/www/comm.360buy.com_test$fastcgi_script_name;
fastcgi_connect_timeout 3;
fastcgi_send_timeout 5;
fastcgi_read_timeout 5;
}
如果加上该行,php如果出现了500,nginx会进行error_page处理,默认不处理
fastcgi_intercept_errors on;
21. Nginx遇到访问aborted解决
当nginx访问所有的aborted都不行时,配置又没有问题,reload也不生效时,然后将所有的nginx进行kill掉,然后重启就好了,
这个是我发现因为我更新了一个动态链接库:
libz.so.1 => /export/servers/hhvm-1.1/support_lib/libz.so.1 (0x00007fd912710000)
然后影响了nginx的运行,所以造成了这种reload无效的问题
发表评论
-
(转)Spring声明式事务配置详解
2013-11-11 16:45 1778Spring声明式事务功能应该是大家应用Spring中使用的最 ... -
(转)Spring mvc+hibernate+freemarker(实战)
2013-10-28 19:40 913http://zz563143188.iteye.com/bl ... -
(转)Java之美[从菜鸟到高手演变]系列之博文阅读导航
2013-10-28 17:00 1656Java之美[从菜鸟到高手演变]系列之博文阅读导航 http: ... -
(转)依赖注入那些事儿
2013-10-25 12:31 618http://www.cnblogs.com/leoo2sk/ ... -
(转)细说业务逻辑
2013-10-25 12:30 481前篇 http://www.cnblogs.com/leoo2 ... -
证书解析与转换
2013-09-26 14:34 0爽哥证书生成步骤: 1. ...
相关推荐
在此个人总结成了这一文档,如下载后别乱转 谢谢) (本手册适用于运维人员需要掌握一定的linux基础) 本文档涉及的主要知识点 环境Redhat6.4 jdk keytool 的使用 openssl 的使用 ssl证书的制作 nginx的安装以及...
自己备份总结,方便给需要的人看
APIGateway(APIGW/API网关),顾名思义,是出现在系统边界上的一个面向API的、串行集中式的强管控服务,这里的边界是企业IT系统的边界,可以理解为企业级应用防火墙,主要起到隔离外部访问与内部系统的作用。...
本文章来总结一下关于apache伪静态与iis伪静态区别介绍,主要讲到了一些规则的问题与配置区别,以后大家就可以直接在iis伪静态转换apache,反之也很简单哦。 在我开时打开模式不一样:apache的.htaccess配置文件是以...
运用面向对象思想和面向服务设计思想完成系统的框架设计,研究以 Zookeeper 作为 Dubbo 服务的注册中心来实现远程服务的调用,并配合使用 Nginx 负载均衡来实现后台服务器的合理调度、研究利用 Mycat 数据库中间件...
App后台开发运维和架构实践》通过阐述移动互联网中 App 后台开发的特点,梳理了 App 后台开发中会遇到的各个技术点,给出了生产环境常用软件的实战运维经验总结,剖析了常见 App 后台技术架构设计,为读者呈现一幅...
博客访问地址: 或者直接打开_posts目录下的markdown文件 ...nginx_install_testNginx的入门 profile_tool_summary系统分析工具总结 simple_ip_stack简单的IP协议栈的实现 linux_timerLinux平台定时器的用法
会结合代码、Wireshark抓包工具还有大量的图解,当然会借鉴一些比较成功的,健壮的代码,如《深入理解计算机系统》中 csapp.c 文件的代码,还会阅读一些开源的项目如Nginx,libevent库等等,同时学习前辈们的编程...
该系列的课程涉及:FFmpeg,WebRTC,SRS,Nginx,Darwin,Live555,等。包括:音视频、流媒体、直播、Android、视频监控28181、等。 我将带领大家一起来学习使用FFmpeg开发视频监控项目,并动手操练。 具体内容包括: 一...
:heavy_dollar_sign: :heavy_dollar_sign: ),我通常会使用最少2个控制节点和4〜5个工作节点,一个用于k3s的HA外部Postgres DB,一个外部LB(Nginx或HAproxy,我现在不需要HA LB)和一个控制节点用作ssh和admin内容...
05 Python 运算符以及总结 06 Python 基本数据类型介绍 07 Python 整形的魔法 08 Python 字符串的魔法 第11章 01 Python 字符串的魔法 02 Python range的用法以及练习 03 Python 课上练习解释 04 Python 基础知识...
【Nginx】Nginx如何配置防止DDOS攻击? 139 限制每秒请求数 139 限制IP连接数 139 白名单设置 139 【数据结构】数组与链表的优缺点 139 【算法】什么是hash? 140 【算法】排序 141 【算法】冒泡排序 141 【算法】...
2.7归纳与总结. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . 51 2.8借shared_ptr 实现copy-on-write. . . . . . . . . . . . . . . . . . . . . . 52 第3章多线程服务器的适用场合与常用...