Nginx反向代理网站,不带www访问域名,竟然返回了Hello Apache!

简介: Nginx反向代理网站,不带www访问域名,竟然返回了Hello Apache!

W]%}75$NFF}~6LJ}(WP7[LW.png

续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第30天,点击查看活动详情


背景


启动 Web 服务,配置好 Nginx 后,刷新配置,通过域名 abc.com 访问(没有写 www ),竟然返回了 Hello Apache! 。。

  • 系统版本

root@iZuf69c5h89bkzv0aqfm8lZ:~# cat /proc/version
Linux version 4.4.0-62-generic (buildd@lcy01-30) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) ) #83-Ubuntu SMP Wed Jan 18 14:10:15 UTC 2017
  • Nginx 配置


server {
        listen 80;
        server_name www.abc.com;
        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $http_host;
                proxy_pass http://127.0.0.1:8888;
        }
}

分析问题


以前也没有用过 Apache ,只是知道曾经的 httpd 作为 HTTP 服务器风靡一时,下面就找下这该死的 Hello Apache! 究竟从何而来。。


# 第一反应,难道有个Apache在运行?
root@iZuf69c5h89bkzv0aqfm8lZ:~# apache -v
-bash: apache: command not found
# 检查了下Nginx的默认页面,没毛病
root@iZuf69c5h89bkzv0aqfm8lZ:~# cat /usr/share/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
# 继续查找httpd或者apache,到这里仍一无所获
root@iZuf69c5h89bkzv0aqfm8lZ:~# which httpd
root@iZuf69c5h89bkzv0aqfm8lZ:~# whereis httpd
httpd:
# 继续查找httpd或者apache,此时发现有个与apache相关的已安装的软件包,哦~ 原来是apache2
root@iZuf69c5h89bkzv0aqfm8lZ:~# dpkg -l | grep httpd
root@iZuf69c5h89bkzv0aqfm8lZ:~# dpkg -l | grep apache
ii  apache2                            2.4.18-2ubuntu3.5                   amd64        Apache HTTP Server
ii  apache2-bin                        2.4.18-2ubuntu3.5                   amd64        Apache HTTP Server (modules and other binary files)
ii  apache2-data                       2.4.18-2ubuntu3.5                   all          Apache HTTP Server (common files)
ii  apache2-utils                      2.4.18-2ubuntu3.5                   amd64        Apache HTTP Server (utility programs for web servers)
# 随手在网上查了下,原来httpd改名为apache2了
root@iZuf69c5h89bkzv0aqfm8lZ:~# which apache2
/usr/sbin/apache2
# 查看apache2版本信息,这台主机也比较久远了,应该是随着Ubuntu系统安装的
root@iZuf69c5h89bkzv0aqfm8lZ:~# apache2 -v
Server version: Apache/2.4.18 (Ubuntu)
Server built:   2017-09-18T15:09:02
# 而且,apache2还是开机自启的
root@iZuf69c5h89bkzv0aqfm8lZ:~# systemctl is-enabled apache2
apache2.service is not a native service, redirecting to systemd-sysv-install
Executing /lib/systemd/systemd-sysv-install is-enabled apache2
enabled
# 经过了解,apache2有个管理工具apachectl,找下它的位置
root@iZuf69c5h89bkzv0aqfm8lZ:~# which apachectl
/usr/sbin/apachectl
# 查看这个文件的内容,可找到关于HTTPD的启动文件路径的一行配置
root@iZuf69c5h89bkzv0aqfm8lZ:~# less /usr/sbin/apachectl
# the path to your httpd binary, including options if necessary
HTTPD=${APACHE_HTTPD:-/usr/sbin/apache2}
# 那么,返回的‘Hello Apache!’到底是从哪里来的?在目录/var/www/html下有个index.html
root@iZuf69c5h89bkzv0aqfm8lZ:~# cat /var/www/html/index.html 
Hello Apache!

解决方法


可在 Nginx 中的 server_name 处同时配置多个域名:


server {
        listen 80;
        server_name www.abc.com abc.com;
        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $http_host;
                proxy_pass http://127.0.0.1:8888;
        }
}

刷新,记得重新加载 Nginx 的配置:


root@iZuf69c5h89bkzv0aqfm8lZ:~# nginx -s reload

一些扩展


这里以顶级域名 abc.com 为例说明A记录中@, www, *等几种域名解析的区别:


解析 说明 访问
abc.com 顶级域名
www.abc.com 二级域名解析,www是一种特殊的二级域名 www.abc.com
@.abc.com 主域名解析,不带主机名的解析 abc.com
*.abc.com 泛域名解析,可解析所有的二级域名 ok.abc.com, 1.abc.com等

Notes: 注意浏览器缓存,最好清除浏览器缓存,或者用一个新的浏览器进行测试,否则,浏览器会自动跳转,尤其当配置了HTTPS时。


nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf


Nginx配置了HTTPS` ,启动报错:


nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:42
提示 Nginx 缺少 http_ssl_module 模块。

解决


  • 查看现有模块

nginx -V

configure arguments: --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi --add-module=/root/FastDFS/fastdfs-nginx-module/src


确实没有 http_ssl_module 模块。。


  • 添加SSL模块


# 进入Nginx源码目录
cd /root/FastDFS/nginx-1.12.2
# 附加--with-http_ssl_module
./configure --prefix=/usr/local/nginx --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/temp/nginx/client --http-proxy-temp-path=/var/temp/nginx/proxy --http-fastcgi-temp-path=/var/temp/nginx/fastcgi --http-uwsgi-temp-path=/var/temp/nginx/uwsgi --http-scgi-temp-path=/var/temp/nginx/scgi --add-module=/root/FastDFS/fastdfs-nginx-module/src --with-http_ssl_module
# 运行make命令
make
# 备份
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
# 停止
nginx -s stop
# 新编译的Nginx覆盖原来的Nginx
cp ./objs/nginx /usr/local/nginx/sbin/
# 启动Nginx
nginx
# 再次查看已安装模块,确认是否成功
nginx -V

http_ssl_module 模块安装成功:


$DJ`4]YOJI[VS~%OY@PXEKR.png

If you have any questions or any bugs are found, please feel free to contact me.

Your comments and suggestions are welcome!

目录
打赏
0
0
0
0
8
分享
相关文章
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
469 60
配置Nginx反向代理实现SSL加密访问的步骤是什么?
我们可以成功地配置 Nginx 反向代理实现 SSL 加密访问,为用户提供更安全、可靠的网络服务。同时,在实际应用中,还需要根据具体情况进行进一步的优化和调整,以满足不同的需求。SSL 加密是网络安全的重要保障,合理配置和维护是确保系统安全稳定运行的关键。
533 60
nginx安装部署ssl证书,同时支持http与https方式访问
为了使HTTP服务支持HTTPS访问,需生成并安装SSL证书,并确保Nginx支持SSL模块。首先,在`/usr/local/nginx`目录下生成RSA密钥、证书申请文件及自签名证书。接着,确认Nginx已安装SSL模块,若未安装则重新编译Nginx加入该模块。最后,编辑`nginx.conf`配置文件,启用并配置HTTPS服务器部分,指定证书路径和监听端口(如20000),保存后重启Nginx完成部署。
2536 8
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
要统计Nginx的客户端IP,可以通过分析Nginx的访问日志文件来实现
633 3
nginx开启局域网https访问
【10月更文挑战第22天】为了调试WebRTC功能,需要在局域网内搭建HTTPS协议。具体步骤包括:在已部署Nginx和安装OpenSSL的环境中生成私钥、证书签名请求和自签名证书;将生成的文件放置到Nginx的证书目录并修改Nginx配置文件,最后重启Nginx服务。注意,自签名证书不受第三方机构认可,如需正式使用,需向CA申请签名。
342 2
阿里云SSL证书不同类型DV、OV和EV如何收费?单域名和通配符SSL价格整理
阿里云SSL证书提供免费和收费版本,涵盖DV、OV、EV多种类型。收费证书品牌包括DigiCert、GlobalSign等,价格从238元/年起。免费SSL证书由Digicert提供,单域名有效3个月,每个实名主体每年可领取20个。具体价格和详情见阿里云SSL官方页面。
2024年阿里云域名热搜词大盘点
2024年阿里云域名热搜词大盘点
157 3
阿里云SSL证书不同类型DV、OV和EV如何收费?单域名和通配符SSL价格整理
阿里云SSL证书提供免费和收费选项。收费证书包括:DV单域名WoSign 238元/年,DigiCert通配符DV 1500元/年,GlobalSign OV企业型1864元/年等。免费SSL证书由Digicert提供,有效期3个月,每年可领取20个单域名证书。更多详情及价格表请参考阿里云官方页面。
阿里云域名注册、续费收费标准价格表及最新优惠口令获取及使用教程参考
阿里云域名注册和续费收费标准在9月份随着全球域名价格的上涨,域名收费标准也做了调整,目前阿里云的.com英文域名的注册价格为83元,续费收费标准为90元,为了让更多用户在注册和续费时价格能更加实惠,阿里云推出了域名优惠口令活动,域名优惠口令适合在域名注册和续费时使用,使用优惠口令通常可以使注册和续费价格减免几元到十几元不等,例如使用优惠口令续费.com域名就可减少5元。本文为大家展示目前阿里云域名注册和续费的最新收费标准以及如何领取和使用域名优惠口令的相关教程,以供参考。
2164 11
非阿里云注册域名如何在云解析DNS设置解析?
非阿里云注册域名如何在云解析DNS设置解析?

热门文章

最新文章

推荐镜像

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问