设置Apache下HTTPS 自动跳转为 HTTP

本文介绍了如何在Apache服务器上设置HTTPS自动跳转到HTTP,以解决HTTPS导致的性能问题。通过启用rewrite_module,配置Apache的SSL文件,并使用RewriteEngine, RewriteCond和RewriteRule等指令实现跳转。详细步骤包括开启rewrite_module,设置RewriteLogLevel,指定RewriteLog目录,以及编写跳转规则。最后讨论了RewriteCond和RewriteRule的执行顺序及浏览器的跳转行为。" 86604862,7487424,Python爬虫常见问题及解决方案,"['Python爬虫', '问题解决', '数据解析']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

还是跟HTTPS相关,因为HTTPS导致系统很慢,频繁崩溃,于是,领导想了个招:能否设置让HTTPS 自动跳转为 HTTP。

网上一查,是可以的。 使用Apache的 rewrite_module模块。

再从网上信手找来几条 rewrite_module 的指令,一配置,嘿,还真跳转了。幸福来得那么容易!但当出现问题时,却是头痛和怀疑不已,哪里错了,哪里不对,是不是HTTPS自动跳转为HTTP不对。这个时候,才在被逼的情况下,耐着性子去查一条条指令的具体用法。

rewrite_module  指令的参考文档(http://httpd.apache.org/docs/current/mod/mod_rewrite.html)。

Apache配置rewrite_module 的方法:

     1、取消注释:LoadModule rewrite_module   (在Apache的配置文件apache.conf中)
      2、.在<VirtualHost>内写下列指令(因是HTTPS自动跳转为HTTP,故在SSL配置文件 ssl.conf 中配置)注意指令中需要空格的地方一定要空格。

              RewriteEngine On                                           #:此处是开关,一定要设置为On;当不需要进行跳转时,可直接设置该指令为Off,而不需要注释掉每行rewrite指令。
              RewriteLogLevel 3                                          #:日志记录级别,为0至8之间。当为0时,表示不记录;

                                                                                          #:参考中说若不是为了调试用,该值不要设置超过2,因为值过大,会比较影响系统性能。
              RewriteLog "D:\www\logs\rewrite.log"        #:日志记录的目录 
              RewriteCond %{HTTP_HOST} !127.0.0.1
              RewriteCond %{SERVER_PORT} 443
              RewriteRule ^/(.*)$ http://%{SERVER_NAME}/$1 [NE]

              #:RewriteCond:定义rewriting会发生的条件,可以有多个。可以用“!” 表示取反。多个 RewriteCond 默认是AND;也可使用[OR]修改各个条件间的关系。

              #:RewriteRule:定义rewrite的规则,可以有多个。格式为:Pattern Substitution [flags] 

                 该指令中第一个参数为:正则表达式形式的条件;

                 第二个参数为:满足条件时,跳转的地址;

                     在<VirtualHost>中,表示 hostname、port  之后、query string 之前的部分。

                     例如 https://127.0.0.1:443/www/index.php?a=123&b=456 中,第二个参数的值应该是/www/index.php

                     其中会用到“$N ”(N为>=0 的正整数)

                         N=0 与 N!=0 的区别:以表达式 RewriteRule ^/(.*)$ http://%{SERVER_NAME}/$1    为例

                         $0 表示替换满足第二个参数Pattern 中的所有部分;如 /www/index.php

                         $1 表示替换满足第二个参数Pattern 中的“()”括号内的部分;如 www/index.php  

                         两者的区别是:$1 未包括Pattern 的括号外的“/”

                第三个参数,参数以“[ ]”包括,各个参数间以“,”分隔。这里详细学习了几个FLAG。

                     [L] LAST  当本条RewriteRule中的条件满足时,只执行本条RewriteRule,不再执行其他的RewriteRule。

                                      注意大条件:当本条RewriteRule中的条件满足时。

                                      在日志文件中,有[L]和无[L]的区别如下图:无[L]时,会再次匹配RewriteRule

                     [R] REDIRECT 显式的强制重定向HTTP。有[R]和无[R]的区别如下:

                      日志中会出现“implicitly forcing redirect (rc=302) with” 的原因 ,查mod_rewrite.c可知,当rewrite 后的地址为URL时,将进行隐式的强制跳转。

                      当设置了标识[R]时,将进行显示的强制跳转。

                   [NE] Not Escape 不对特殊字符进行转义,当不设置该参数时,在log中能看到这样的记录:                           

                  其他指令:RewriteBase  定义rewrite的目录范围

使用rewrite_module 进行跳转后,浏览器第一次返回 FOUND 302 的结果,表示临时性跳转;接着浏览器会自动再继续跳转,返回 200 实现URL的正常跳转。

也就是说,使用rewrite_mode 时,浏览器会自动进行两次动作,一次是实现地址的转换,另一次是实现地址的跳转。

RewriteCond 和 RewriteRule 的执行顺序:先执行 RewriteRule,再去查看是否满足 RewriteCond 的条件。 该点是从log中看出的:

             

                        

在Java中,我们可以利用Apache POI库处理DOCX文档,然后通过documents4j将其转换为PDF,并结合java.nio.file包来操作文件和目录。以下是一个基本的示例代码,展示了如何创建一个工具类来完成这个任务: ```java import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.docx4j.jaxb.Context; import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.wml.P; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import java.io.File; import java.io.FileOutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; public class DocumentConverter { private static final String DOCX_FORMAT = ".docx"; private static final String PDF_FORMAT = ".pdf"; public void convertDocxToPdf(String sourceFolder, String targetFolder) { File sourceDir = new File(sourceFolder); if (!sourceDir.exists() || !sourceDir.isDirectory()) { throw new IllegalArgumentException("Invalid source folder path."); } File targetDir = new File(targetFolder); if (!targetDir.exists() || !targetDir.mkdir()) { throw new RuntimeException("Failed to create target folder."); } List<String> existingFiles = getExistingFiles(sourceFolder, DOCX_FORMAT); for (String fileName : existingFiles) { try { Resource docxResource = new FileSystemResource(new File(sourceFolder + File.separator + fileName)); XWPFDocument document = new XWPFDocument(docxResource.getInputStream()); // Create the pdf file path based on the original docx name Path pdfPath = Paths.get(targetFolder, fileName.replace(DOCX_FORMAT, PDF_FORMAT)); if (!pdfPath.toFile().exists()) { Context context = Context.createForCurrentThread(); context.loadModels(); P bodyParagraph = document.getBody().getFirstChild(); // Convert content and save as PDF documents4j.convert(bodyParagraph, new FileOutputStream(pdfPath.toFile())); System.out.println("Converted " + fileName + " to " + pdfPath); } else { System.out.println(fileName + " already converted, skipping..."); } } catch (IllegalArgumentException | Docx4JException e) { System.err.println("Error converting " + fileName + ": " + e.getMessage()); } } } private List<String> getExistingFiles(String folder, String extension) { List<String> result = new ArrayList<>(); Files.walk(Paths.get(folder)) .filter(path -> path.toString().endsWith(extension)) .forEach(path -> result.add(path.subpath(folder.length(), path.getNameCount()))); return result; } } ``` 在这个代码中,我们首先检查输入的源文件夹是否存在且是否为文件夹,接着创建目标文件夹。然后遍历源文件夹中的所有DOCX文件,对每个DOCX文件执行转换操作。如果目标PDF文件已经存在,则跳过;如果不存在则进行转换。 注意:这只是一个基础示例,实际生产环境中可能需要添加错误处理、日志记录以及资源释放等更复杂的功能。同时,由于Apache POI和documents4j处理大文档可能会有性能问题,对于大量文件转换,可以考虑异步处理或者分批处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值