Linux操作篇之LAMP(二)

d、域名跳转(实现域名跳转有两种实现方式)

d.1、代码实现

<?php
header('HTTP/1.1 301 Moved Permanently');
header('Location: http:/www.hahahahh.com/');
?>

这样就可以跳转到www.hahahahh.com这一域名

d.2、使用.htaccess来实现

301是指跳转状态码:301代表永久跳转;302代表临时跳转。一般情况下设置为301

 

 

4、apache+openssl以实现https传输

4.1、配置SSL证书

openssl genrsa -out ca.key 2048

使用openssl产生私钥  genrsa(使用RSA算法生成私钥)  -out ca.key (输出到ca.key文件)  2048(私钥的长度)

openssl req -new -key ca.key -out test.csr

需要依次输入所属国家、地区、城市、组织、组织单位、email等信息(Common Name这个最好填写域名)

openssl x509 -req -days 365 -sha256 -in test.csr  -signkey ca.key  -out test.crt

使用CA服务器签发证书,设置有效期为一年,使用sha256加密方式。test.crt是客户端安装

 

4.2、修改配置文件

/etc/httpd/conf.d/ssl.conf

 

 /etc/httpd/conf.d/VirtualHost.conf

/etc/httpd/conf/httpd.conf(添加以下代码可以在访问http的时候,自动跳转到https)

开启转发规则

检查访问端口不是443的访问

使其使用https重新访问

 

五、日志切割及缓存管理(若设置的为http,则修改/etc/httpd/conf/httpd.conf;若设置的https,则修改/etc/httpd/conf.d/ssl.conf)

 

5.1、切割日志。一般是将访问时的日志以每天/小时分开记录,方便在出现问题的时候排查。

错误日志记录(将原本的  ErrorLog "logs/error_log"  修改为  Errorlog  "|/usr/sbin/rotatelogs -I  //etc/httpd/logs/error_%Y%m%d.log 86400")

访问日志记录(将原本的  CustomLog "logs/access_log" combined  修改为  Customlog "|/usr/sbin/rotatelogs -I  //etc/httpd/logs/access_%Y%m%d.log 86400"  combined)

-I 是校准时区为UTC     86400单位是秒,等于一天,相当于一天执行一次

 

5.2、只记录日志中的某些信息,不记录访问图片的访问信息

在生效的目录下添加(SetEnvIf  Request_URI  ".*\.jpg$"  image-request)

修改记录访问日志为(Customlog "|/usr/sbin/rotatelogs -I  //etc/httpd/logs/access_%Y%m%d.log 86400"  combined  env=!image-request)

将被访问的jpg格式(或其他格式)的图片都定义为image-request,然后在日志记录时,不记录带有这个标签的信息。

 

5.3、静态缓存处理

目的是将图片、js、css等静态文件缓存到用户的本地电脑上(设置期限),从而当用户再次请求时,不需要再次进行下载,从而提高了访问速度。

A、使用expires模块(添加到主配置文件)

<IfModule mod_expires>
  ExpiresActive on

  ExpiresByType image/gif “access plus 1 day ”

  ExpiresByType text/css “now plus 2 hours”

  ExpiresByType application/x-javascript “now plus 2 hours”

  ExpiresDefault “now plus 0 min”
</IfModule>


B、使用headers模块(添加到主配置文件)

<IfModule mod_headers>

  将htm、html、txt文件缓存时间为一天

  <filesmatch "\.(htm|html|txt)$">

    header set cache-control "max-age 86400"

  </filesmatch>

  将js、css等文件缓存时间为一小时

  <filesmatch "\.(js|css)$">

    header set cache-control "max-age 3600"

  </filesmatch>

</IfModule>

 

六、禁止某个目录下解析文件

<Directory "/var/www/html/test">

  php_admin_flag engine off

  <filesmatch "(.*)PHP">

    Order deny , allow

    Deny from all

  </filesmatch>

</Directory>

posted on 2021-03-06 15:00  沫仔  阅读(63)  评论(0编辑  收藏  举报

导航