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

本文涉及的产品
云解析 DNS,旗舰版 1个月
全局流量管理 GTM,标准版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 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!

目录
相关文章
|
8月前
|
域名解析 网络协议 安全
在Linux中,想在命令行下访问某个网站,并且该网站域名还没有解析,如何做?
在Linux中,想在命令行下访问某个网站,并且该网站域名还没有解析,如何做?
|
1月前
|
安全 网络协议 网络安全
只有IP地址没有域名,如何实现HTTPS访问?
在仅有IP地址而无域名的情况下,实现HTTPS访问并非不可能。主要挑战包括证书颁发机构(CA)对IP地址的支持有限及浏览器兼容性问题。解决方案有:1) 搭建私有CA为内部IP地址颁发证书;2) 使用支持IP地址的公共CA服务。选择合适的方案需根据需求权衡。具体步骤包括选择证书类型、生成CSR文件、提交并完成验证、安装SSL证书和配置强制HTTPS访问。确保IP地址稳定,并定期维护安全性。 **申请优惠**:访问JoySSL官网并填写注册码“230907”可优惠申请IP地址证书。
214 5
|
1月前
|
域名解析 弹性计算 应用服务中间件
使用域名访问部署在ECS上的网站
本文为您介绍如何为网站配置域名并为域名配置HTTPS证书。
|
8月前
|
运维 Serverless 网络安全
函数计算产品使用问题之通过仓库导入应用时无法配置域名外网访问,该如何排查
函数计算产品作为一种事件驱动的全托管计算服务,让用户能够专注于业务逻辑的编写,而无需关心底层服务器的管理与运维。你可以有效地利用函数计算产品来支撑各类应用场景,从简单的数据处理到复杂的业务逻辑,实现快速、高效、低成本的云上部署与运维。以下是一些关于使用函数计算产品的合集和要点,帮助你更好地理解和应用这一服务。
|
4月前
|
负载均衡 Ubuntu 应用服务中间件
nginx修改网站默认根目录及发布(linux、centos、ubuntu)openEuler软件源repo站点
通过合理配置 Nginx,我们可以高效地管理和发布软件源,为用户提供稳定可靠的服务。
327 13
|
5月前
|
网络协议 安全 文件存储
动态DNS(DDNS)技术在当前网络环境中日益重要,它允许使用动态IP地址的设备通过固定域名访问
动态DNS(DDNS)技术在当前网络环境中日益重要,它允许使用动态IP地址的设备通过固定域名访问,即使IP地址变化,也能通过DDNS服务保持连接。适用于家庭网络远程访问设备及企业临时或移动设备管理,提供便捷性和灵活性。示例代码展示了如何使用Python实现基本的DDNS更新。尽管存在服务可靠性和安全性挑战,DDNS仍极大提升了网络资源的利用效率。
448 6
|
5月前
|
域名解析 网络协议 前端开发
浏览器输入域名网址访问后的过程详解
1、以91处理网为例,客户端浏览器通过DNS解析到www.91chuli.com,IP地址是202.108.22.5,通过这个IP地址找到客户端到服务器的路径。客户端浏览器发起一个HTTP会话到202.108.22.5,然后通过TCP进行封装数据包,输入到网络层。
156 2
|
6月前
|
安全 应用服务中间件 Shell
nginx配置https的ssl证书和域名
nginx配置https的ssl证书和域名
|
6月前
|
Docker 容器
docker nginx-proxy 添加自定义https网站
docker nginx-proxy 添加自定义https网站
80 4
|
6月前
|
前端开发 JavaScript 应用服务中间件
使用nginx部署网站
使用nginx部署网站

推荐镜像

更多