老程序员分享: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

相关文章
|
1月前
|
编解码 应用服务中间件 Linux
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
147 1
|
19天前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
168 18
|
21天前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
162 17
|
2月前
|
数据建模 应用服务中间件 PHP
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
本示例演示如何通过Docker挂载同一宿主目录至Nginx与PHP容器,实现PHP项目运行环境配置。需注意PHP容器中监听地址修改为0.0.0.0:9000,并调整Nginx配置中fastcgi_pass指向正确的IP与端口。同时确保Nginx容器中/var/www/html权限正确,以避免访问问题。
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
|
3月前
|
应用服务中间件 网络安全 nginx
配置Nginx以支持Websocket连接的方法。
通过上述配置,Nginx将能够理解WebSocket协议的特殊要求,代理Websocket流量到合适的后端服务器。注意,Websocket并不是HTTP,尽管它最初是通过HTTP请求启动的连接升级,因此保证Nginx了解并能够妥善处理这种升级流程是关键。
589 10
|
2月前
|
Ubuntu 应用服务中间件 Linux
在Ubuntu上配置Nginx实现开机自启功能
至此,Nginx应该已经被正确地设置为开机自启。在Ubuntu中利用 `systemd`对服务进行管理是一种高效的方式,为系统管理员提供了强大的服务管理能力,包括但不限于启动、停止、重启服务,以及配置服务的开机自启动。通过这些简洁的命令,即使是对Linux不太熟悉的用户也能轻松地进行配置。
113 0
|
4月前
|
安全 应用服务中间件 网络安全
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
264 0
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
|
负载均衡 Java 应用服务中间件
程序员的nginx技能包(1)——nginx是啥,对程序员有啥用
nginx是啥 nginx就是个服务器,做Java Web的童鞋可能比较了解Tomcat、做dotNet Web的童鞋可能比较了解IIS、还有非常出名的apache服务器等等等等。
146 0
|
5月前
|
应用服务中间件 Linux 网络安全
Centos 8.0中Nginx配置文件和https正书添加配置
这是一份Nginx配置文件,包含HTTP与HTTPS服务设置。主要功能如下:1) 将HTTP(80端口)请求重定向至HTTPS(443端口),增强安全性;2) 配置SSL证书,支持TLSv1.1至TLSv1.3协议;3) 使用uWSGI与后端应用通信(如Django);4) 静态文件托管路径设为`/root/code/static/`;5) 定制错误页面(404、50x)。适用于Web应用部署场景。
638 87
|
5月前
|
负载均衡 应用服务中间件 nginx
Nginx配置与命令
Nginx 是一款高性能的 HTTP 和反向代理服务器,其配置文件灵活且功能强大。本文介绍了 Nginx 配置的基础结构和常用指令,包括全局块、Events 块、HTTP 块及 Server 块的配置方法,以及静态资源服务、反向代理、负载均衡、HTTPS 和 URL 重写等功能实现。此外,还提供了常用的 Nginx 命令操作,如启动、停止、重载配置和日志管理等,帮助用户高效管理和优化服务器性能。