老程序员分享:Nginx高级配置

简介: 老程序员分享:Nginx高级配置

           Nginx高级配置-官网logo之favicon.ico


                                          作者:尹正杰


版权声明:原创作品,谢绝转载!否则将追究法律责任。


一.浏览器会默认帮咱们访问官网的图标


1>.浏览器访问网站""


2>.使用curl命令访问


【root@node108.yinzhengjie.org.cn ~】# curl


<!DOCTYPE html

[/span>html

[/span>head

[/span>meta charset="UTF-8"

[/span>title

[/span>style

.hello{


color: red;


font-size: 30px;


font-family: "curlz mt","华文彩云","arial", "微软雅黑";


}




[/span>body

[/span>p class="hello"



【root@node108.yinzhengjie.org.cn ~】#


3>.可以看到后台是有日志输出的


【root@node101.yinzhengjie.org.cn ~】# > /yinzhengjie/softwares/nginx/logs/access_json.log             #在访问之前,建议大家把日志清空,看的比较明显哈~当然,我说的是测试环境。


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# tail -10f /yinzhengjie/softwares/nginx/logs/access_json.log        #可以看到浏览器的访问记录被记录了404错误信息,而使用命令行curl访问的确是200正常访问。


{"@timestamp":"2019-12-22T11:10:40+08:00","host":"172.30.1.101","clientip":"172.30.1.254","size":571,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":


"node101.yinzhengjie.org.cn","uri":"/favicon.ico","domain":"node101.yinzhengjie.org.cn","xff":"-","referer":"","tcp_xff":"",


"http_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36","status":"404"}


{"@timestamp":"2019-12-22T11:47:35+08:00","host":"172.30.1.101","clientip":"172.30.1.101","size":362,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":


"node101.yinzhengjie.org.cn","uri":"/index.html","domain":"node101.yinzhengjie.org.cn","xff":"-","referer":"-","tcp_xff":"","http_user_agent":"curl/7.29.0","status":"200"}


3>.访问虚拟主机"",发现也出现类似的问题


4>.访问虚拟主机"",发现也出现类似的问题


5>.访问"favicon.ico"依旧会出现问题


  favicon.ico文件是浏览器收藏网址时显示的图标,当客户端使用浏览器问页面时,浏览器会自己主动发起请求获取页面的favicon.ico文件,但是当浏览器请求的favicon.ico文件不存在时,服务器会记录404日志,而且浏览器也会显示404报错。


二.关于favicon.ico的解决办法


  既然知道favicon.ico是啥了,那就好办了,找一个公司logo放上去就ok啦。


1>.从互联网上下载一个logo咱们自己来用于模拟试验


【root@node101.yinzhengjie.org.cn ~】# ll


total 0


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# wget


--2019-12-22 12:05:35--


Resolving ()... 220.194.105.131, 2408:8710:20:1140:8000::3


Connecting to ()|220.194.105.131|:443... connected.


HTTP request sent, awaiting response... 200 OK


Length: 25214 (25K) 【image/x-icon】


Saving to: ‘favicon.ico’


100%【==================================================================================================================================>】 25,214 --.-K/s in 0.002s


2019-12-22 12:05:35 (10.0 MB/s) - ‘favicon.ico’ saved 【25214/25214】


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# ll


total 28


-rw-r--r-- 1 root root 25214 Mar 25 2016 favicon.ico


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# wget 下载京东的logo


【root@node101.yinzhengjie.org.cn ~】# ll


total 28


-rw-r--r-- 1 root root 25214 Mar 25 2016 favicon.ico


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn //代码效果参考:http://www.jhylw.com.cn/312126119.html

~】#

【root@node101.yinzhengjie.org.cn ~】# wget


--2019-12-22 12:07:01--


Resolving img.alicdn.com (img.alicdn.com)... 111.206.4.120, 111.206.4.115


Connecting to img.alicdn.com (img.alicdn.com)|111.206.4.120|:443... connected.


HTTP request sent, awaiting response... 200 OK


Length: 4540 (4.4K) 【image/png】


Saving to: ‘T1OjaVFl4dXXa.JOZB-114-114.png’


100%【==================================================================================================================================>】 4,540 --.-K/s in 0s


2019-12-22 12:07:01 (1.07 GB/s) - ‘T1OjaVFl4dXXa.JOZB-114-114.png’ saved 【4540/4540】


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# ll


total 36


-rw-r--r-- 1 root root 25214 Mar 25 2016 favicon.ico


-rw-r--r-- 1 root root 4540 May 29 2017 //代码效果参考:http://www.jhylw.com.cn/594025921.html

T1OjaVFl4dXXa.JOZB-114-114.png

【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# wget 下载淘宝的logo


2>.编辑子配置文件


【root@node101.yinzhengjie.org.cn ~】# cat /yinzhengjie/softwares/nginx/conf.d/https.conf               #我们把京东的logo放在虚拟主机为""中


server {


listen 80;


listen 443 ssl;


server_name


location / {


root /yinzhengjie/data/web/nginx/static;


index index.html;


}


location = /favicon.ico {


root /yinzhengjie/data/web/nginx/images/jd;


}


}


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# nginx -t


nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok


nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# cat /yinzhengjie/softwares/nginx/conf.d/mobile.conf               #我们将淘宝下载的logo放在虚拟主机为"mobile.yinzhengjie.org.cn"中


server {


listen 80;


listen 443 ssl;


server_name mobile.yinzhengjie.org.cn;


ssl_certificate /yinzhengjie/softwares/nginx/certs/mobile.yinzhengjie.org.cn.crt;


ssl_certificate_key /yinzhengjie/softwares/nginx/certs/mobile.yinzhengjie.org.cn.key;


ssl_session_cache shared:sslcache:20m;


ssl_session_timeout 10m;


location / {


root /yinzhengjie/data/web/nginx/mobile;


index index.html;


}


location /favicon.ico {


root /yinzhengjie/data/web/nginx/images/taobao;


}


}


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# nginx -t


nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok


nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful


【root@node101.yinzhengjie.org.cn ~】#


3>.将下载的图片放在指定路径中


【root@node101.yinzhengjie.org.cn ~】# mkdir -pv /yinzhengjie/data/web/nginx/images/{jd,taobao{


mkdir: created directory ‘/yinzhengjie/data/web/nginx/images’


mkdir: created directory ‘/yinzhengjie/data/web/nginx/images/jd’


mkdir: created directory ‘/yinzhengjie/data/web/nginx/images/taobao’


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# ll


total 36


-rw-r--r-- 1 root root 25214 Mar 25 2016 favicon.ico


-rw-r--r-- 1 root root 4540 May 29 2017 T1OjaVFl4dXXa.JOZB-114-114.png


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# mv favicon.ico /yinzhengjie/data/web/nginx/images/jd/


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# mv T1OjaVFl4dXXa.JOZB-114-114.png /yinzhengjie/data/web/nginx/images/taobao/favicon.ico


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# ll


total 0


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# ll /yinzhengjie/data/web/nginx/images/ -R


/yinzhengjie/data/web/nginx/images/:


total 0


drwxr-xr-x 2 root root 25 Dec 22 12:46 jd


drwxr-xr-x 2 root root 25 Dec 22 12:47 taobao


/yinzhengjie/data/web/nginx/images/jd:


total 28


-rw-r--r-- 1 root root 25214 Mar 25 2016 favicon.ico


/yinzhengjie/data/web/nginx/images/taobao:


total 8


-rw-r--r-- 1 root root 4540 May 29 2017 favicon.ico


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】#


4>.重新加载nginx的配置文件


【root@node101.yinzhengjie.org.cn ~】# netstat -untalp | grep nginx


tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 12283/nginx: master


tcp 0 0 0.0.0.0:443 0.0.0.0: LISTEN 12283/nginx: master


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# ps -ef | grep nginx | grep -v grep


root 12283 1 0 12:30 ? 00:00:00 nginx: master process nginx


nginx 12284 12283 0 12:30 ? 00:00:00 nginx: worker process


nginx 12285 12283 0 12:30 ? 00:00:00 nginx: worker process


nginx 12286 12283 0 12:30 ? 00:00:00 nginx: worker process


nginx 12287 12283 0 12:30 ? 00:00:00 nginx: worker process


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# nginx -s reload


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# ps -ef | grep nginx | grep -v grep


root 12283 1 0 12:30 ? 00:00:00 nginx: master process nginx


nginx 12406 12283 6 12:52 ? 00:00:00 nginx: worker process


nginx 12407 12283 5 12:52 ? 00:00:00 nginx: worker process


nginx 12408 12283 5 12:52 ? 00:00:00 nginx: worker process


nginx 12409 12283 5 12:52 ? 00:00:00 nginx: worker process


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】# netstat -untalp | grep nginx


tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 12283/nginx: master


tcp 0 0 0.0.0.0:443 0.0.0.0: LISTEN 12283/nginx: master


【root@node101.yinzhengjie.org.cn ~】#


【root@node101.yinzhengjie.org.cn ~】#


5>.访问虚拟主机"",发现logo可以正常显示啦


6>.访问虚拟主机"",发现logo是可以正常显示啦


当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。


欢迎加入基础架构自动化运维:598432640,大数据SRE进阶之路:959042252,DevOps进阶之路:526991186

目录
打赏
0
0
0
0
43
分享
相关文章
Nginx进程配置指令详解
Nginx进程配置指令主要包括:`worker_processes`设置工作进程数;`worker_cpu_affinity`绑定CPU核心;`worker_rlimit_nofile`设置最大文件描述符数量;`worker_priority`设置进程优先级;`worker_connections`设置最大连接数;`daemon`控制守护进程模式;`master_process`启用主进程模式;`pid`设置PID文件路径;`user`指定用户和组;`error_log`配置错误日志。这些指令在`nginx.conf`中配置,用于优化和控制Nginx的运行行为。
90 10
nginx配置证书和私钥进行SSL通信验证
nginx配置证书和私钥进行SSL通信验证
159 4
配置Nginx反向代理时如何指定后端服务器的权重?
配置Nginx反向代理时如何指定后端服务器的权重?
313 61
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
如何测试Nginx反向代理实现SSL加密访问的配置是否正确?
344 60
配置Nginx反向代理实现SSL加密访问的步骤是什么?
我们可以成功地配置 Nginx 反向代理实现 SSL 加密访问,为用户提供更安全、可靠的网络服务。同时,在实际应用中,还需要根据具体情况进行进一步的优化和调整,以满足不同的需求。SSL 加密是网络安全的重要保障,合理配置和维护是确保系统安全稳定运行的关键。
430 60
nginx反向代理bucket目录配置
该配置实现通过Nginx代理访问阿里云OSS存储桶中的图片资源。当用户访问代理域名下的图片URL(如 `http://代理域名/123.png`)时,Nginx会将请求转发到指定的OSS存储桶地址,并重写路径为 `/prod/files/2024/12/12/123.png`。
191 5
负载均衡指南:Nginx与HAProxy的配置与优化
负载均衡指南:Nginx与HAProxy的配置与优化
312 3
运维系列.Nginx配置中的高级指令和流程控制
运维系列.Nginx配置中的高级指令和流程控制
392 1
Nginx 配置指令的执行顺序(学习笔记二十)
大多数 Nginx 新手都会频繁遇到这样一个困惑,那就是当同一个location配置块使用了多个 Nginx 模块的配置指令时,这些指令的执行顺序很可能会跟它们的书写顺序大相径庭。
1911 0