Discuz X3.2如何配置https(附视频,附代码示例)

作者:FancyPig | 发布时间: | 更新时间:

视频讲解 配置说明 随着HTTPS逐渐全球化,站长们可能更加需要HTTPS的部署,鉴于Discuz X3.3还有诸多不稳定因素,Discuz Fans漏洞百出,最终看来目前为止Discuz X3.2还是最稳定,最适合运营的,当然你如果没事干去捣鼓捣鼓其他的版本我没意见~ 这里带来的是针对Discuz X3.2 的HTTPS配置教程 【1】服务器前端的配置,点击图片查看 【2】Discuz 判断服务器是否使用 SSL 的修补和完善Discuz 判断服务器是否使用 SSL 的代码不适合 Nginx+CGI 的情况,即 PHP-FPM 。 这个时候我们需要修改以下文件: Discuz 采用 `$_SERVER[‘ HTTPS ’] `的方式来判断 SSL ,但是因为我的 VPS 架构问题( nginx+php-fpm ),无法采用这种方式识别,所以需要对 Discuz 程序进行一些调整(使用 `$_SERVER[‘ SERVER_PORT ’]` 来判断)。 source/class/discuz/discuz_application.php (约第 187 行处): 查找: ``` $_G['isHTTPS'] = ($_SERVER['HTTPS'] && strtolower($_SERVER['HTTPS']) != 'off') ? true : false; ``` 修改为 ``` $_G['isHTTPS'] = ($_SERVER['SERVER_PORT'] == 443 || $_SERVER['HTTPS'] && strtolower($_SERVER['HTTPS']) != 'off') ? true : false; ``` uc_server/avatar.php (约第 13 行处)查找: ``` define('UC_API', strtolower(($_SERVER['HTTPS'] == 'on' ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/')))); ``` 修改为: ``` define('UC_API', strtolower(($_SERVER['SERVER_PORT'] == 443 || $_SERVER['HTTPS'] == 'on' ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/')))); ``` 【3】除去非 HTTPS 内容避免提示“不安全内容” Source/plugin/manyou/Service/DiscuzTips.php ,最后的那段 JS 加载脚本删除就行。 非楼主层如果有点评,那么点评者头像不是 HTTPS 开头,也需要修改一个文件来适配: template/default/forum/viewthread_node_body.htm (约 180 行),搜索 div class="psta vm";,将下面一行注释掉或者删除。 打开浏览器,使用开发者工具或者查看源码逐一排查加载的非 HTTPS 资源并修改。 (温馨提示:大部分99%情况都是要修改template/你的模板文件/header.htm或者footer.htm) 【4】后台设置的修改完善 在后台还有一些设置,可能会干扰 https 的使用 后台 >全局 >站点 URL ,改为 https 开头的 后台 >站长 >UCenter 设置 > UCenter 访问地址,修改为 https 开头的 UCenter 后台 >应用管理 >应用的主 URL ,修改为 https 开头。修改后可能会显示通讯失败,如果 UC 和论坛程序安装在同一机器,此失败可无视,实测可以和 UC 正常通讯不影响(测试是否正常通讯程序的 Bug ),如果 UC 和论坛程序不在一台机器上,有可能不能通讯。 另外在 后台 > 全局 > 域名设置 中的一些设置也可能使 https 失效,如果更新缓存后论坛默认连接还是 HTTP ,请删除 后台 > 全局 > 域名设置 > 应用域名 > 默认 里面的默认域名(一般去 forum.php 尾巴这里会有内容,为了 HTTPS 请删除)。 【5】数据库的调整 ``` UPDATE pre_forum_post SET message=REPLACE(message,'http://你的网址','https://你的网址'); ``` 需要注意的是,出于安全考虑, Discuz 后台默认情况下禁止 SQL 语句直接执行,只能使用常用 SQL 当中的内容,如果想自己随意书写 SQL 升级语句,需要将程序文件 config/config_global.php 当中的$_config[admincp][runquery] 设置修改为 1 。 【6】关于头像修改失败,请点击下方图片查看 Discuz X3.2如何配置https(附视频,附代码示例)-站长帮-成就更好的站长 【7】关于头像的链接还是http的问题 打开./uc_server/avatar.php ``` header('Location: '.UC_API.'/'.$avatar_url); ``` 修改成 ``` header('Location: '.'https://你的域名/uc_server'.'/'.$avatar_url); ``` 【8】常见代码示例 针对nginx+apache双引擎的可以修改conf文件,参考以下代码: ``` server { listen 80; server_name iculture.org.cn bbs.iculture.org.cn; root /www/web/iculture_org_cn/public_html; index index.html index.php index.htm;rewrite ^/((?!admin|mobcent).*)$ https://bbs.iculture.org.cn/$1 permanent; error_page 400 /errpage/400.html; error_page 403 /errpage/403.html; error_page 404 /errpage/404.html; error_page 405 /errpage/405.html; location ~* \.(png|gif|jpeg|jpg)$ { # valid_referers none blocked iculture.org.cn bbs.iculture.org.cn iculture.org.cn ; # if ($invalid_referer) { # rewrite ^/ http://bbs.iculture.org.cn/static/image/common/disallow.png; #} } location ~ \.php$ { proxy_pass http://127.0.0.1:88; include naproxy.conf; } location / { try_files $uri @apache; } location @apache { proxy_pass http://127.0.0.1:88; include naproxy.conf; } } server { listen 443 ssl ; server_name iculture.org.cn bbs.iculture.org.cn; ssl on; ssl_certificate /www/web/iculture_org_cn/key/1_bbs.iculture.org.cn_bundle.crt; ssl_certificate_key /www/web/iculture_org_cn/key/2_bbs.iculture.org.cn.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5; ssl_prefer_server_ciphers on; ssl_session_timeout 10m; ssl_session_cache builtin:1000 shared:SSL:10m; ssl_buffer_size 1400; #add_header Strict-Transport-Security max-age=15768000; ssl_stapling on; ssl_stapling_verify on; rewrite ^/(admin.*(?=plugins|cloudaddons).*)$ http://bbs.iculture.org.cn/$1 redirect; if ( $host != 'bbs.iculture.org.cn' ) { rewrite "^/(.*)$" https://bbs.iculture.org.cn/$1 permanent; } rewrite ^([^\.]*)/v/(.*)$ http://$2 break;root /www/web/iculture_org_cn/public_html; index index.html index.php index.htm; error_page 400 /errpage/400.html; error_page 403 /errpage/403.html; error_page 404 /errpage/404.html; error_page 405 /errpage/405.html; location ~* \.(png|gif|jpeg|jpg)$ { # valid_referers none blocked iculture.org.cn bbs.iculture.org.cn iculture.org.cn ; #if ($invalid_referer) { # rewrite ^/ http://bbs.iculture.org.cn/static/image/common/disallow.png; #} } location ~ \.php$ { proxy_pass http://127.0.0.1:88; include naproxy.conf; } location / { try_files $uri @apache; } location @apache { proxy_pass http://127.0.0.1:88; include naproxy.conf; } } ``` 针对nginx引擎的可以修改conf文件,参考以下代码: ``` server { #listen 80; listen 443 ssl; server_name iculture.org.cn bbs.iculture.org.cn; root /www/web/iculture_org_cn/public_html; index forum.php index.php index.html index.htm; ssl on; ssl_certificate /www/web/www_iculture_org_cn/key/iculture.pem; ssl_certificate_key /www/web/www_iculture_org_cn/key/iculture.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; #rewrite ^/(admin.*(?=plugins|cloudaddons).*)$ http://bbs.iculture.org.cn/$1 redirect; #if ( $host != 'bbs.iculture.org.cn' ) { # rewrite "^/(.*)$" https://bbs.iculture.org.cn/$1 permanent; #} # set a nice expire for assets location ~* "^.+\.(jpe?g|gif|css|png|js|ico|pdf|zip|tar|t?gz|mp3|wav|swf)$" { expires 30d; log_not_found off; } location ~ .*\.(php|php5)?$ { fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_read_timeout 600; } } server { listen 80; server_name iculture.org.cn bbs.iculture.org.cn; #rewrite ^/((?!admin|mobcent).*)$ https://bbs.iculture.org.cn/$1 permanent; #location / { # if ($request_uri !~ ^/admin(/)?.*){ # rewrite ^(.*) https://$server_name$1 permanent; # } #} rewrite ^(.*) https://$server_name$1 permanent; } ```
标签:discuz, discuz开启HTTPS, HTTPS部署