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

本文涉及的产品
云解析 DNS,旗舰版 1个月
云解析DNS,个人版 1个月
全局流量管理 GTM,标准版 1个月
简介: 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!

目录
相关文章
|
5天前
|
JavaScript 前端开发 应用服务中间件
Nginx——一个域名下部署多个Vue项目
如何在同一域名下部署第二个Vue项目而不影响现有项目:更新`vue.config.js`,设置`publicPath`为`/screen/`。修改Vue Router的`base`为`screen`。在Nginx配置中添加新location `/screen`,指向第二项目`dist`目录。测试访问`http://&lt;域名&gt;/screen/`。别忘了检查并修复任何遗漏的配置,如数据看板默认设置。
17 2
|
8天前
|
前端开发 应用服务中间件 网络安全
nginx和apache的区别
Nginx是轻量级、抗并发的服务器,擅长静态文件处理和反向代理,配置简洁,适合高流量场景。 Apache采用同步多进程模型,功能丰富,对动态请求处理强,SSL支持好,适合复杂的企业级应用。 根据需求,高并发选Nginx,丰富功能和稳定性考虑Apache。两者也可结合使用,Nginx作为前端代理,Apache处理后端请求。
|
14天前
|
网络协议 应用服务中间件 网络安全
如何排查Nginx配置问题导致的域名访问错误
如何排查Nginx配置问题导致的域名访问错误
28 2
|
19天前
|
应用服务中间件 Apache nginx
apache、nginx开启rewrite重写服务及伪静态
apache、nginx开启rewrite重写服务及伪静态
34 4
|
20天前
Discuz 手机版访问自动跳转到手机域名
Discuz 手机版访问自动跳转到手机域名
23 1
|
22天前
|
弹性计算 应用服务中间件 Linux
双剑合璧:在同一ECS服务器上共存Apache与Nginx的实战攻略
在ECS服务器上同时部署Apache和Nginx的实战:安装更新系统,Ubuntu用`sudo apt install apache2 nginx`,CentOS用`sudo yum install httpd nginx`。配置Nginx作为反向代理,处理静态内容及转发动态请求到Apache(监听8080端口)。调整Apache的`ports.conf`监听8080。重启服务测试,实现两者高效协同,提升Web服务性能。记得根据流量和需求优化配置。【6月更文挑战第21天】
165 1
|
1月前
|
运维 监控 Serverless
Serverless 应用引擎产品使用合集之函数没有绑定自定义域名,如何在公网访问该函数
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
|
10天前
|
Web App开发
软件开发常见流程之移动端调试方法,利用Chrome(谷歌浏览器)的模拟手机调试,搭建本地Web服务器,手机和服务器在一个局域网,通过手机访问服务器,使用服务器,利用ip实现域名访问
软件开发常见流程之移动端调试方法,利用Chrome(谷歌浏览器)的模拟手机调试,搭建本地Web服务器,手机和服务器在一个局域网,通过手机访问服务器,使用服务器,利用ip实现域名访问
|
12天前
|
前端开发
若依修改,前端部署最初的样子,Ngnix部署最初的模样,配置域名/,就能够访问,最初的Ngnix的配置是怎样写的
若依修改,前端部署最初的样子,Ngnix部署最初的模样,配置域名/,就能够访问,最初的Ngnix的配置是怎样写的
|
1月前
|
存储 运维 Serverless
Serverless 应用引擎产品使用合集之为SD函数配置域名并添加路径/sd后无法正常访问,如何配置自定义域名
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。

推荐镜像

更多