标签归档:apache

apache下载zip文件没有content-length

同事做的一个东西,遇到一个问题。http下载一个apache提供服务的一个文件,css,html有content-length(可能是文件较小,是把结果显示的?),但是zip文件没有。导致无法做下载的进度条。网上搜了一下,也有人反应这种情况。
生猛的解决办法是注释掉
LoadModule deflate_module modules/mod_deflate.so
当然,这个会影响其他的应用,因为这个是zip包,不需要再压缩了,找到了下面的语句,增加了zip,问题也得到了解决。
# Don’t compress images
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|zip)$ no-gzip dont-vary

原因就是服务器支持压缩,对zip类的文件禁用该功能就行了。

如何用apache的url_rewrite实现强制https访问

apache的url_rewrite是个神器,有时候一些难以解决的问题,用他就迎刃而解了。
现在的需求是一个服务器的某个文件或者某个目录里的文件,要强制https访问。
用如下的代码

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

如果有别的需求,可以加载rewritecond里。

freebsd升级mysql client

mysql serve 是5.1.x的,客户端是 5.0.x的,用phpmyadmin,总有一个提示,说版本不匹配,感觉不爽。
用Ports的方法升级客户端,不行,报错。说5.1和5.0有冲突,只能先删除。
用了 portmaster -o databases/mysql51-client mysql-client-5.0.89
也不行。
加上 export CONFLICTS=” 后执行,还是不行。看了
pkg_info mysql\*

Required by:
php5-mysql-5.2.12
php5-mysqli-5.2.12
因为还跑着网站,就没敢贸然行事。
这天趁网站人少的时候,干了一把
删除老的package
#pkg_delete -f mysql-client-5.0.89
到mysql5.1 client的目录
#cd /usr/ports/databases/mysql51-client

#make install clean
。。
。。
等待
。。
好了看一下版本
#mysql -V
mysql Ver 14.14 Distrib 5.1.42, for portbld-freebsd8.0 (amd64) using 5.2
用portmaster升级2个有依赖的php组件
#portmaster php5-mysql php5-mysqli
等待


重启apache,好了。
出乎意料的是安装过程中,php可以正常的链接mysql。网站正常,还以为pkg_delete会删除使用的so文件,导致网站出状况。

感谢hshh

用RCS把httpd.conf版本管理起来

apache或者其他的配置文件经常会修改。每次改了什么有时候会忘掉。或者没有备份。
用RCS 版本控制系统,Revision Control System(很古老的UNIX应用程序)的ci,co来管理,很是方便。简单的就三个命令。
checkout出来httpd.conf
co -l httpd.conf
修改后,看看都修改了什么
rcsdiff httpd.conf
如果确认,并且好用,就checkin
ci -u -wjuhui httpd.conf
目录里会有一个 httpd.conf,v 文件,是文本文件,可以打开看看。

感谢asong。

apache目录密码保护错误

apache的.htaccess有个可以保护目录的功能。当用户访问这个目录里资源的时候,会弹出对话框,让输入用户名和口令。在设置的时候有一些关必要条件。比如启用.htaccess文件等。
这2天在freebsd里面想这么做一次,但是遇到了问题,一直报错
configuration error: couldn’t check user. No user file?
后来经过调整,还有
configuration error: couldn’t check access. No groups file?

Invalid command ‘AuthUserFile’, perhaps misspelled or defined by a module not included in the server configuration
等错误信息。

后来经过n多个小时。。n>3,最后准备投降的时候,看到httpd.conf里面有一些注释掉名字里面有auth字样的module。一狠心,全去掉注释,好了!
又经过一番测试。
需要启用下面的三个module

LoadModule authn_file_module libexec/apache22/mod_authn_file.so
LoadModule authz_user_module libexec/apache22/mod_authz_user.so
LoadModule auth_basic_module libexec/apache22/mod_auth_basic.so

当然我用的是
AuthType Basic
.htaccess的内容如下

AuthType Basic
AuthName "admin"
AuthUserFile /usr/local/etc/apache22/.htpasswd
Require user admin

怎么用url_rewrite添加www

有时候baidu,google收录的链接域名不一致,有些带www,有些又不带。
这时候用apache的url_rewrite做一下301转向处理,会比较好。代码如下

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.juyimeng\.com$ [NC]
RewriteRule .? http://www.juyimeng.com%{REQUEST_URI} [R=301,L]

让二级域名也起效

RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+\.)?juyimeng\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1juyimeng.com%{REQUEST_URI} [R=301,L]

只把juyimeng.com的链接转向

RewriteEngine on
RewriteCond %{HTTP_HOST} ^juyimeng\.com$ [NC]
RewriteRule .? http://www.juyimeng.com%{REQUEST_URI} [R=301,L]

更多关于apache url_rewrite的文章,请参考本站其他相关文章

apache child pid exit signal File size limit exceeded error

一个正常使用的网站,LAMP架构,突然打不开了,但是另外一个二级域名的网站,在同一台服务器上可以打开,静态文件也可以打开,因为出故障前有段时间程序没有更新,程序错误的可能性不大。
telnet 到网站的80端口,也可以打开,现象很奇怪。
重起了apache,没用,mysql用命令行也可以使用,没问题,一时没了方向。
于是tail了apache的log,一个是正常的web log,一个是error_log。
观察之后,发现了异常现象,apache的error_log一直输出错误信息如下:

[Thu May 24 23:11:27 2007] [notice] child pid 23406 exit signal File size limit exceeded (25)
[Thu May 24 23:12:01 2007] [notice] child pid 23544 exit signal File size limit exceeded (25)
[Thu May 24 23:12:47 2007] [notice] child pid 23633 exit signal File size limit exceeded (25)
[Thu May 24 23:12:53 2007] [notice] child pid 22799 exit signal File size limit exceeded (25)

说有文件大小超过了限制。
google了一下,说是超过了apache 2G的文件限制,试用find命令在系统里面查找超大的文件

find / -size +1000000k

查找到2个文件,一个是

/xxx/xxx/page_parse_time.log
/xxx/yyy/support-popbytes_log

第一个文件page_parse_time.log的大小在2G左右,压缩备份后,

> /xxx/xxx/page_parse_time.log

,问题解决。
page_parse_time.log的文件内容是程序记录sql query的log以及执行时间的log,每个页面请求都会写上一些数据,这么超过2G也不奇怪了。

上次还遇到过一次mysql数据条数过多,的问题
报错信息如下:

LOGSQL Insert Failed: insert into adodb_logsql (created,sql0,sql1,params,tracer,timer) values( NOW(),?,?,?,?,?) The table 'adodb_logsql' is full 

经查

mysql> select count(*) from adodb_logsql ;
 +----------+ 
| count(*) |
 +----------+ 
| 33127244 | 
+----------+

3千万,,,可能是有点大了,最后truncate了这个表,问题解决
可是最后查mysql的文档,关于Scalability and Limits部分没有说mysql最多可以存多少条记录,估计跟表的类型也相关。原文说的是
Handles large databases. We use MySQL Server with databases that contain 50 million records. We also know of users who use MySQL Server with 60,000 tables and about 5,000,000,000 rows.
只是不知道它说的50 million是一个database里面的所有记录还是所有表的记录加起来。后边的应该是所有60,000个表加起来的数据量。
————————
结论,必要的日志是需要的,但是要控制大小,有循环机制,或者有监控机制,切记,切记。

如何做301转向

301 redirect: 301代表永久性转移(Permanently Moved),301重定向是网页更改地址后对搜索引擎友好(SEO)的最好方法,只要不是暂时搬移的情况,都建议使用301来做转址。

现在知道的有2类,一类是在web服务软件上做,还有就是在程序里面做

先说在web服务软件

如果是iis:
打开internet信息服务管理器,在要重定向的网页或目录上按右键

  1. 选中“重定向到URL”
  2. 在对话框中输入目标页面的地址
  3. 选中“资源的永久重定向”
  4. 点击“应用

如果是apache方法多一些,也方便一些
如果要把访问www.juhui.com的请求全部301到www.juyimeng.com

<VirtualHost www.juhui.com>
Redirect 301 / http://www.juyimeng.com/
CustomLog logs/redir "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
</VirtualHost>

上面的
Redirect 301 / http://www.juyimeng.com/
可以写为
Redirect permanent / http://www.juyimeng.com/

也可以用apache的.htaccess,怎么使.htaccess起效,点这里
.htaccess文件的内容

RewriteEngine on
rewriterule abc\.html /index\.html [R=301]

其总用是把abc.html301到index.html

用程序来做

用PHP来做301重定向

<?
Header( "HTTP/1.1 301 Moved Permanently" ); 
Header( "Location: http://www.test.com" ); 
?> 

用JSP (Java)来做301重定向

<%
response.setStatus(301);
response.setHeader( "Location", "http://www.test.com/" );
response.setHeader( "Connection", "close" );
%> 

用CGI PERL 来做301重定向

$q = new CGI;
print $q->redirect("http://www.test.com/"); 

用ASP来做301重定向

<%@ Language=VBScript %>
<% Response.Status="301 Moved Permanently"; Response.AddHeader("Location","http://www.test.com/"); %>
用ColdFusion 来做301重定向

<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://www.test.com"> 用ASP .NET Redirect来做301重定向

用Ruby on Rails Redirect来做301重定向

def old_action
headers[“Status”] = “301 Moved Permanently”
redirect_to “http://www.test.com/”
end

html的meta refresh和javascript也可以转向

<meta http-equiv="Refresh" content="0; url=http://test.com/page.html">

<script>
window.location="http://www.test.com/test.html";
</script>

但是上述的方法不推荐

如果要Redirect的源url是html这种结尾也可以,改apache的配置,把html扩展名的文件当Php或者其他脚本来处理就可以了
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm

上述改造是否成功,可以用httpwatch来查看

apache tips

apache如何多个域名指向一个site?
用ServerAlias

<VirtualHost www.juyimeng.com>
ServerName www.juyimeng.com
ServerAdmin juhui@test.com
ServerAlias www.abc.com
ServerAlias www.def.com
DocumentRoot /usr/juhui/htdocs/
ErrorLog logs/juyimenng.error_log
</VirtualHost> 

如何不用.htaccess这个文件名字?

AccessFileName .config
如何让.htaccess文件起作用?
如果.htaccess文件不起作用,请检查apache的配置文件里面的
AllowOverride 设置
简单的用
AllowOverride All
最少要是
AllowOverride FileInfo

通过在客户端缓存css.js,图片等文件来加速网站浏览速度

ExpiresActive On
ExpiresByType image/gif A259200
ExpiresByType image/png A259200
ExpiresByType image/jpg A259200
ExpiresByType image/jpeg A259200
ExpiresByType text/css A259200
ExpiresByType application/x-javascript A259200
259200是3天的秒数,A的意思是从最后一个访问开始,缓存三天。

推荐一个apache+mysql+php+java等的安装文件包http://www.apachefriends.org/
方便得很,直接集成多种服务,包括
Apache、MySQL、PHP + PEAR、Perl、mod_php、mod_perl、mod_ssl、OpenSSL、phpMyAdmin、Webalizer、Mercury Mail Transport System for Win32 and NetWare Systems v3.32、JpGraph、FileZilla FTP Server、mcrypt、eAccelerator、SQLite 和 WEB-DAV + mod_auth_mysql
等,你要用的有,想到的有,没想到的也有好,测试,本地开发环境使用,谁用谁知道阿。