设为首页 收藏本站
查看: 800|回复: 0

[经验分享] apache配置rewrite及.htaccess文件

[复制链接]

尚未签到

发表于 2018-11-17 08:54:36 | 显示全部楼层 |阅读模式
  业务需求:单独部署Apache服务来做图片服务器

1.环境:
  1.1腾讯云主机,初始化安装环境,最小化安装centos7.4 x86_64位
  系统版本:
  

[root@VM_82_178_centos conf]# cat /etc/redhat-release
  
CentOS Linux>  

  1.2httpd服务安装方式:yum 安装
  yum install httpd -y
  httpd源码包下载地址:
  http://archive.apache.org/dist/httpd/
  httpd服务版本:
  

[root@VM_82_178_centos conf]# httpd -v  
Server version: Apache/2.4.6 (CentOS)
  
Server built:   Jun 27 2018 13:48:59
  

  提示:只需要安装apache服务足够提供图片服务器应用了
  1.3开启Rewrite功能模块:
  说明:在centos7的系统上,yum安装httpd服务,默认安装的apache服务的版本是2.4.6,它默认已经安装好了支持Rewrite功能模块
  

[root@VM_82_178_centos ~]# find / -name mod_rewrite.so  
/usr/lib64/httpd/modules/mod_rewrite.so
  

  补充:在centos6的系统上,yum安装httpd服务,默认安装的apache服务的版本是2.2.15
  

并且默认已经在配置文件/etc/httpd/conf.modules.d/00-base.conf 中加载了这个模块LoadModule rewrite_module modules/mod_rewrite.so  
[root@VM_82_178_centos conf.modules.d]# grep mod_rewrite.so /etc/httpd/conf.modules.d/00-base.conf
  
LoadModule rewrite_module modules/mod_rewrite.so
  

  并且Apache/2.4.6 默认支持的模块为101个,而apache2.2.x以下版本默认才支持61个模块

2、apache不同版本开启rewrite功能介绍:
  在apache1.x版本和apache2.2.x版本以及apache2.4.x开启rewrite功能的方式是不一样的

2.1、 Apache 1.x
  Apache 1.x 的用户请检查 conf/httpd.conf 中是否存在如下两段代码:
  

LoadModule Rewrite_module libexec/mod_Rewrite.so  AddModule mod_Rewrite.c
  

  Apache 1.x版本配置Apache配置文件httpd.conf实现rewrite
  找到
  

  

  

  

  没有的自己添加,然后之间写入你的rewrite规则,例如:
  

  
RewriteEngine On
  
RewriteRule ^q/[0-9]+/(.*)$  /uploads/picture/$1 [L]
  

  

  

  DocumentRoot "/data/www/images"
  ServerName  img.yb898.cn
  ServerAlias img.yb898.cn
  ErrorLog "logs/img.yb898.cn-error_log"
  CustomLog "logs/img.yb898.cn-access_log" common
  
  #Options Indexes FollowSymlinks
  Options  FollowSymlinks
  ##Allowoverride All
  Allowoverride None
  Order Allow,Deny
  Allow from all
  
  RewriteEngine On
  RewriteRule ^q/[0-9]+/(.*)$  /uploads/picture/$1 [L]
  
  
  

  

  注意:如果网站使用通过虚拟主机来定义,请务必加到虚拟主机配置,即  中去,如果加在虚拟主机配置外部将可能无法使用

2.2、 Apache 2.2.15
  Apache 2.2.15版本的用户请检查 conf/httpd.conf 中是否存在如下一段代码:
  

LoadModule Rewrite_module modules/mod_rewrite.so  

  注:如果前面有#,将其去掉。并且保证你的apache文件里有mod_rewrite.so文件(1.X版的要有mod_rewrite.c)。
  

  DocumentRoot "/data/www/images"
  ServerName  img.yb898.cn
  ServerAlias img.yb898.cn
  ErrorLog "logs/img.yb898.cn-error_log"
  CustomLog "logs/img.yb898.cn-access_log" common
  
  #Options Indexes FollowSymlinks
  Options  FollowSymlinks
  ##Allowoverride All
  Allowoverride None
  Order Allow,Deny
  Allow from all
  
  

  

2.3  apache2.4.6版本:
  (已经测试验证是成功的)
  

  

  DocumentRoot "/data/www/images"
  ServerName  img.yb898.cn
  ServerAlias img.yb898.cn
  ErrorLog "logs/img.yb898.cn-error_log"
  CustomLog "logs/img.yb898.cn-access_log" common
  
  #Options Indexes FollowSymlinks
  Options  FollowSymlinks
  Require all granted
  RewriteEngine on
  RewriteRule ^q/[0-9]+/(.*)$  /uploads/picture/$1 [L]
  RewriteRule ^[0-9]+.([0-9]+).([0-9]+).([0-9]+).(.*)$     /uploads/picture/$1/$2/$3/$4 [L]
  
  

  

  浏览器请求链接:
  http://img.yb898.cn/q/12345/2016/07/28/1520924032.png
  http://img.yb898.cn/12345.2016.07.28/1520924032.png
  http://img.yb898.cn/uploads/picture/2016/07/28/1520924032.png
  文件存放路径:
  

[root@VM_82_178_centos images]# ll /data/www/images/uploads/picture/2016/07/28/1520924032.png  
-rw-r--r-- 1 root root 89032 Mar 13 14:53 /data/www/images/uploads/picture/2016/07/28/1520924032.png
  

  提示:
  一个常见的问题:配置完rewrite后有时会显示Forbidden  You don't have permission to access /data/www/images on this server.这样的错误。
  加上Options FollowSymLinks就行了,注意不能加Indexes,这个表示显示目录(根目录没有index.html的情况下就显示文件夹的目录结构)。

3.让apache支持.htaccess

3.1对于apache1.x的版本:
  

把  
RewriteEngine On
  
RewriteRule ^q/[0-9]+/(.*)$  /uploads/picture/$1 [L]
  

  
里的代码删除掉
  

  

  找到#AccessFileName .htaccess把前面的#注释去掉
  找到:
  

  DocumentRoot "/data/www/images"
  ServerName  img.yb898.cn
  ServerAlias img.yb898.cn
  ErrorLog "logs/img.yb898.cn-error_log"
  CustomLog "logs/img.yb898.cn-access_log" common
  
  #Options Indexes FollowSymlinks
  Options  FollowSymlinks
  #Allow Override All
  Allowoverride None
  Order Allow,Deny
  Allow from all
  
  

  

  

  把里边的AllowOverride None  改为  Allow Override All   这样.htaccess文件才能启用。

3.2、对于apache2.x的版本
  找到#AccessFileName .htaccess把前面的#注释去掉
  找到:
  

  DocumentRoot "/data/www/images"
  ServerName  img.yb898.cn
  ServerAlias img.yb898.cn
  ErrorLog "logs/img.yb898.cn-error_log"
  CustomLog "logs/img.yb898.cn-access_log" common
  
  #Options Indexes FollowSymlinks
  Options  FollowSymlinks
  Allowoverride All
  Require all granted
  
  

  

  把参数  Allowoverride All添加上,这样.htaccess文件才能启用。

3.3、针对apache2.4.6举例:
  

RewriteEngine on  
RewriteRule ^q/[0-9]+/(.*)$  /uploads/picture/$1 [L]
  

  直接把上面的代码写入到.htaccess文件,并且把.htaccess文件放到站点或者图片的根目录下,重启apache就行了。
  

[root@VM_82_178_centos images]# cat  /data/www/images/.htaccess  
RewriteEngine on
  
RewriteRule ^q/[0-9]+/(.*)$  /uploads/picture/$1 [L]
  
RewriteRule ^[0-9]+.([0-9]+).([0-9]+).([0-9]+).(.*)$     /uploads/picture/$1/$2/$3/$4 [L]
  

  为了安全起见,在httpd.conf文件添加如下参数禁止.htaccess文件通过浏览器请求被下载
  

  Require all denied
  #Require all granted
  

  

  

  一般的apache以上配置就行了,但我的还不行,所以研究了一下apache的配置文件httpd.conf发现了这样一个参数:
  
  AccessFileName ht.access
  意思是:AccessFileName定义每个目录下的访问控制文件的文件名,缺省为.htaccess (大多数人直接就是.htaccess,所以好多网上教程都没写这步,而我的是ht.access),可以通过更改这个文件,来改变不同目录的访问控制限制。
  将ht.access改为 .htaccess 重启apache就行了。
  AccessFileName .htaccess
  题外:.htaccess位置问题:htaccess文件(或者"分布式配置文件")提供了针对每个目录改变配置的方法,即在一个特定的目录中放置一个包含指令的文件,其中的指令作用于此目录及其所有子目录。(每一个文件夹下都可以有个.htaccess文件)
  同时rewrite规则写入到.htaccess 配置文件的一个好处就是:在每次新添加规则到.htaccess 文件中时,apache服务不需要重启,就直接可以生效
  httpd.conf去掉注释参数如下:亲测可用
  [root@VM_82_178_centos ~]# grep -vE '^#|^$'  /etc/httpd/conf/httpd.conf
  

ServerRoot "/etc/httpd"  
Include conf.modules.d/*.conf
  
User apache
  
Group apache
  
ServerAdmin root@localhost
  
ServerName 127.0.0.1:80
  

  AllowOverride none
  #Require all denied
  Require all granted
  

  
DocumentRoot "/var/www/html"
  

  Options Indexes FollowSymLinks
  AllowOverride None
  Require all granted
  

  

  DirectoryIndex index.html index.html.var
  

  
AccessFileName .htaccess
  

  Require all denied
  #Require all granted
  

  
ErrorLog "logs/error_log"
  
LogLevel warn
  

  LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
  LogFormat "%h %l %u %t \"%r\" %>s %b" common
  
  # You need to enable mod_logio.c to use %I and %O
  LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
  
  CustomLog "logs/access_log" combined
  

  

  ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
  

  

  AllowOverride None
  Options None
  Require all granted
  

  

  TypesConfig /etc/mime.types
  AddType application/x-compress .Z
  AddType application/x-gzip .gz .tgz
  AddType application/x-httpd-php .asp .py .pl .bop .foo .133t .jsp .com
  AddType text/html .shtml
  AddOutputFilter INCLUDES .shtml
  

  
AddDefaultCharset UTF-8
  

  #
  MIMEMagicFile conf/magic
  

  
EnableSendfile on
  
IncludeOptional conf.d/*.conf
  
Servername 0.0.0.0:80
  
Listen 80
  

  
ServerAdmin webmaster@dianrui.com
  
DocumentRoot /var/www/html
  
ServerName 119.29.97.131
  
ServerAlias 119.29.97.131
  

  

  DocumentRoot "/data/www/images"
  ServerName  img.yb898.cn
  ServerAlias img.yb898.cn
  ErrorLog "logs/img.yb898.cn-error_log"
  CustomLog "logs/img.yb898.cn-access_log" common
  
  #Options Indexes FollowSymlinks
  Options  FollowSymlinks
  Allowoverride All
  Require all granted
  
  

  
[root@VM_82_178_centos ~]#
  

4.httpd.conf开启IncludeOptional参数
  配置文件/etc/httpd/conf/httpd.conf中开启IncludeOptional参数,使每个vhost虚拟主机单独 写到一个配置文件中
  

  
[root@VM_82_178_centos vhost]# grep IncludeOptional /etc/httpd/conf/httpd.conf
  
IncludeOptional conf.d/*.conf
  
IncludeOptional conf/vhost/*.conf
  
[root@VM_82_178_centos vhost]# grep -C 10 IncludeOptional /etc/httpd/conf/httpd.conf
  

  MIMEMagicFile conf/magic
  

  
#EnableMMAP and EnableSendfile: On systems that support it,
  
EnableSendfile on
  

  
IncludeOptional conf.d/*.conf
  
IncludeOptional conf/vhost/*.conf
  
Servername 0.0.0.0:80
  

  
Listen 80
  

  
ServerAdmin webmaster@dianrui.com
  
DocumentRoot /var/www/html
  
ServerName 119.29.97.131
  
ServerAlias 119.29.97.131
  

  

[root@VM_82_178_centos vhost]# cat /etc/httpd/conf/vhost/img.yb898.cn.conf  

  DocumentRoot "/data/www/images"
  ServerName  img.yb898.cn
  ServerAlias img.yb898.cn
  ErrorLog "logs/img.yb898.cn-error_log"
  CustomLog "logs/img.yb898.cn-access_log" common
  
  #Options Indexes FollowSymlinks
  Options  FollowSymlinks
  Allowoverride All
  Require all granted
  
  

  

  centos7行httpd服务的相关的 启动命令如下:
  

  
[root@VM_82_178_centos vhost]# systemctl status httpd.service

  
[root@VM_82_178_centos vhost]# systemctl>  
[root@VM_82_178_centos vhost]# systemctl start httpd.service
  
[root@VM_82_178_centos vhost]# systemctl stop httpd.service
  
[root@VM_82_178_centos vhost]# httpd -t
  
Syntax OK
  
[root@VM_82_178_centos vhost]# apachectl  configtest
  
Syntax OK
  
[root@VM_82_178_centos vhost]# apachectl  start
  
[root@VM_82_178_centos vhost]# apachectl  stop
  
[root@VM_82_178_centos vhost]# apachectl  restart
  

  查看apache服务状态:
  

[root@VM_82_178_centos vhost]# systemctl status httpd.service  
● httpd.service - The Apache HTTP Server
  Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Active: active (running) since Wed 2018-08-29 16:36:20 CST; 6s ago
  Docs: man:httpd(8)
  man:apachectl(8)
  Main PID: 11789 (httpd)
  Status: "Processing requests..."
  CGroup: /system.slice/httpd.service
  ├─11789 /usr/sbin/httpd -DFOREGROUND
  ├─11790 /usr/sbin/httpd -DFOREGROUND
  ├─11791 /usr/sbin/httpd -DFOREGROUND
  ├─11792 /usr/sbin/httpd -DFOREGROUND
  ├─11793 /usr/sbin/httpd -DFOREGROUND
  └─11794 /usr/sbin/httpd -DFOREGROUND
  

  
Aug 29 16:36:20 VM_82_178_centos systemd[1]: Starting The Apache HTTP Server...
  
Aug 29 16:36:20 VM_82_178_centos systemd[1]: Started The Apache HTTP Server.
  

  测试apache服务:
  http://img.yb898.cn/q/12345/2016/07/28/1520924032.png
  http://img.yb898.cn/12345.2016.07.28/1520924032.png
  http://img.yb898.cn/uploads/picture/2016/07/28/1520924032.png
  参考文档:
  https://www.cnblogs.com/CheeseZH/p/5169352.html
  https://www.cnblogs.com/mrcln/p/5635515.html
  https://www.cnblogs.com/miketwais/p/mod_rewrite.html



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-636034-1-1.html 上篇帖子: apache安装mod_security模块(ubuntu编译安装) 下篇帖子: Apache 网页与安全优化
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表