怎麼透過Nginx定義Header頭訊息
透過修改nginx的conf文件,輕鬆達到自訂HTTP Header的目的。
Nginx 使用 ngx_headers_more 模組來增加、刪除出站、入站的 Header 資訊。預設模組沒有加入到 Nginx 的原始碼中,要使用相關功能需要在編譯 Nginx 時加入模組。本人伺服器中的Nginx 在編譯時沒有加入該模組,使用-V 查看當前Nginx 的編譯參數:
[root@z-dig ~]# nginx -V nginx version: www.z-dig.com built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --user=www --group=www \ --with-http_ssl_module --with-http_stub_status_module [root@z-dig ~]#
從官網下載模組:
[root@z-dig ~]# cd /usr/local/src/ [root@z-dig src]# wget 、https://codeload.github.com/openresty/headers-more-nginx-module/zip/master\ -O ./headers-more-nginx-module-master.zip [root@z-dig src]# unzip headers-more-nginx-module-master.zip
重新編譯Nginx 前,請求www. z-dig.com 的Header 訊息:
[root@KVM ~]# curl -I www.z-dig.com HTTP/1.1 200 OK Server: www.z-dig.com Date: Sat, 23 Apr 2016 11:25:15 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-Powered-By: PHP/5.6.17 Vary: Accept-Encoding, Cookie Cache-Control: max-age=3, must-revalidate WP-Super-Cache: Served supercache file from PHP [root@KVM ~]#
現在重新編譯Nginx ,平滑更新:
[root@z-dig ~]# cd /usr/local/src/nginx [root@z-dig nginx]# make clean rm -rf Makefile objs [root@z-dig nginx]#./configure --prefix=/usr/local/nginx --user=www --group=www \ --with-http_ssl_module --with-http_stub_status_module \ --add-module=/usr/local/src/headers-more-nginx-module-master [root@z-dig nginx]# make [root@z-dig nginx]# make install [root@z-dig nginx]# kill -s USR2 `cat /usr/local/nginx/logs/nginx.pid` [root@z-dig nginx]# ps -ef|grep nginx root 2017 1 0 Apr21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx www 2018 2017 0 Apr21 ? 00:00:30 nginx: worker process root 21717 2017 0 19:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx www 21718 21717 0 19:41 ? 00:00:00 nginx: worker process root 21856 18312 0 19:45 pts/2 00:00:00 grep nginx [root@z-dig nginx]# kill -s WINCH `cat /usr/local/nginx/logs/nginx.pid.oldbin` [root@z-dig nginx]# ps -ef|grep nginx root 2017 1 0 Apr21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx root 21717 2017 0 19:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx www 21718 21717 0 19:41 ? 00:00:00 nginx: worker process root 21943 18312 0 19:49 pts/2 00:00:00 grep nginx [root@z-dig nginx]# kill -s QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin` [root@z-dig nginx]# ps -ef|grep nginx root 21717 1 0 19:41 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx www 21718 21717 0 19:41 ? 00:00:00 nginx: worker process root 22050 18312 0 19:54 pts/2 00:00:00 grep nginx [root@z-dig nginx]#
到此Nginx 已重新編譯並平滑升級成功。
在Nginx 的設定檔中加入程式碼,將先前要求網站傳回Header 中的X-Powered-By 和WP-Super-Cache 刪除:
more_clear_headers 'X-Powered-By'; more_clear_headers 'WP-Super-Cache'; [root@z-dig ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@z-dig ~]# nginx -s reload
再次要求查看效果:
[root@KVM ~]# curl -I www.z-dig.com HTTP/1.1 200 OK Server: www.z-dig.com Date: Sat, 23 Apr 2016 12:03:04 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive Vary: Accept-Encoding, Cookie Cache-Control: max-age=3, must-revalidate [root@KVM ~]#
經過測試已成功將請求傳回中的Header 指定訊息刪除。想了解 ngx_headers_more 的其他功能請造訪專案官網。
以上是怎麼透過Nginx定義Header頭訊息的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

如何解決 Nginx 403 Forbidden 錯誤?檢查文件或目錄權限;2. 檢查 .htaccess 文件;3. 檢查 Nginx 配置文件;4. 重啟 Nginx。其他可能原因還包括防火牆規則、SELinux 設置或應用程序問題。

服務器無權訪問所請求的資源,導致 nginx 403 錯誤。解決方法包括:檢查文件權限。檢查 .htaccess 配置。檢查 nginx 配置。配置 SELinux 權限。檢查防火牆規則。排除其他原因,如瀏覽器問題、服務器故障或其他可能的錯誤。

在 Linux 中啟動 Nginx 的步驟:檢查 Nginx 是否已安裝。使用 systemctl start nginx 啟動 Nginx 服務。使用 systemctl enable nginx 啟用在系統啟動時自動啟動 Nginx。使用 systemctl status nginx 驗證啟動是否成功。在 Web 瀏覽器中訪問 http://localhost 查看默認歡迎頁面。

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

問題的答案:304 Not Modified 錯誤表示瀏覽器已緩存客戶端請求的最新資源版本。解決方案:1. 清除瀏覽器緩存;2. 禁用瀏覽器緩存;3. 配置 Nginx 允許客戶端緩存;4. 檢查文件權限;5. 檢查文件哈希;6. 禁用 CDN 或反向代理緩存;7. 重啟 Nginx。

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

如何配置 Nginx 進行負載均衡?定義上游服務器池,指定服務器 IP 和端口。定義虛擬主機,監聽連接並轉發到上游池。指定位置,匹配請求並轉發到上游池。

查看 Nginx 運行狀態的方法有:使用 ps 命令查看進程狀態;查看 Nginx 配置文件 /etc/nginx/nginx.conf;使用 Nginx 狀態模塊啟用狀態端點;使用 Prometheus、Zabbix 或 Nagios 等監控工具。
