1.主設定檔與虛擬主機分離
如果虛擬主機很多的話,進行分離看起來會更方便,還可以按功能、業務進行劃分,下面以兩個虛擬主機為例。
完整的除去空行和註解後的設定檔:
[root@nginx-01 conf]# egrep -v "#|^$" nginx.conf.bak worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
建立/app/nginx/conf目錄下虛擬主機設定目錄
mkdir extra
利用server模組建立www和bbs兩個虛擬網站
[root@nginx-01 conf]# cat -n nginx.conf [root@nginx-01 conf]# sed -n '10,20p' nginx.conf server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
www網站
[root@nginx-01 conf]# cat extra/www.conf server { listen 80; server_name www.yygg.com; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
bbs網站
[root@nginx-01 conf]# cat extra/bbs.conf server { listen 80; server_name bbs.yygg.com; location / { root html/bbs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html/bbs; } }
主設定檔配置(nginx.conf)
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/bbs.conf; }
#檢查設定
[root@nginx-01 conf]# /app/nginx/sbin/nginx -t nginx: the configuration file /app/nginx-1.18.0//conf/nginx.conf syntax is ok nginx: configuration file /app/nginx-1.18.0//conf/nginx.conf test is successful
建立網站目錄
[root@nginx-01 conf]# mkdir /app/nginx/html/{www,bbs} [root@nginx-01 conf]# echo "http://www.yygg.com" >>/app/nginx/html/www/index.html [root@nginx-01 conf]# echo "http://bbs.yygg.com" >>/app/nginx/html/bbs/index.html [root@nginx-01 conf]# echo "192.168.1.5 www.yygg.com bbs.yygg.com" >>/etc/hosts
啟動服務並測試
[root@nginx-01 conf]# /app/nginx/sbin/nginx [root@nginx-01 conf]# curl www.yygg.com http://www.yygg.com [root@nginx-01 conf]# curl bbs.yygg.com http://bbs.yygg.com
2.虛擬主機別名設定
以www網站為例,設定別名
所謂別名就是除了主網域外額外設定一個或多個網域
為www.yygg.com
設定別名yygg.com
。
[root@nginx-01 conf]# cat extra/www.conf server { listen 80; server_name www.yygg.com yygg.com; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html/www; } }
重啟nginx測試
[root@nginx-01 conf]# /app/nginx/sbin/nginx -s reload [root@nginx-01 conf]# cat /etc/hosts 192.168.1.5 www.yygg.com bbs.yygg.com yygg.com [root@nginx-01 conf]# curl yygg.com http://www.yygg.com
3.Nginx status狀態資訊配置
狀態資訊記錄使用的是`ngx_http_stub_status_module`模組實作
#/app/nginx/sbin/nginx -V檢查編譯是否有上述模組:
nginx version: nginx/1.18.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --user=nginx --group=nginx --prefix=/app/nginx-1.18.0/ --with-http_stub_status_module --with-http_ssl_module建立一個status的虛擬主機,方式參考標題1,
status.conf#配置檔案如下:
server { listen 80; server_name status.yygg.com; location / { stub_status on; access_log off; } }主設定檔
nginx.conf
追蹤status虛擬主機設定sed -i '11 i include extra/status.conf;' nginx.conf檢查語法並重新啟動nginx
/app/nginx/sbin/nginx -t /app/nginx/sbin/nginx -s reload設定hosts解析
192.168.1.5 status.yygg.com
status.yygg.com
查看Writing ##返回給客戶端的Header資訊數[root@nginx-01 conf]# curl status.yygg.com Active connections: 1 server accepts handled requests 4 4 4 Reading: 0 Writing: 1 Waiting: 0顯示結果解析:
Reading ##讀取到客戶端的Header資訊數
#Active connections: 1 ##正處理的連線數為1
server ##共處理了4次連線
accepts ##總共建立了4次握手
handled requests ##共處理了4次請求
Waiting ##NGinx已經處理完正在等候下一次請求的指令的駐留連數字
4.增加錯誤日誌error_log語法:
error_log file level;
#關鍵字不變,file是日誌存放位置,level是錯誤日誌等級通常只用warn|error|crit三個等級
配置錯誤日誌配置,在
nging.conf
worker_processes 1;下新增
error_loglogs/error_log;
######沒錯,就這一行! ###以上是Nginx安裝後常用功能如何設定的詳細內容。更多資訊請關注PHP中文網其他相關文章!

NGINX可用於提升網站性能、安全性和可擴展性。 1)作為反向代理和負載均衡器,NGINX可優化後端服務和分擔流量。 2)通過事件驅動和異步架構,NGINX高效處理高並發連接。 3)配置文件允許靈活定義規則,如靜態文件服務和負載均衡。 4)優化建議包括啟用Gzip壓縮、使用緩存和調整worker進程。

NGINXUnit支持多種編程語言,通過模塊化設計實現。 1.加載語言模塊:根據配置文件加載相應模塊。 2.應用啟動:調用語言運行時執行應用代碼。 3.請求處理:將請求轉發給應用實例。 4.響應返回:將處理後的響應返回給客戶端。

NGINX和Apache各有優劣,適合不同場景。 1.NGINX適合高並發和低資源消耗場景。 2.Apache適合需要復雜配置和豐富模塊的場景。通過比較它們的核心特性、性能差異和最佳實踐,可以幫助你選擇最適合需求的服務器軟件。

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

要關閉 Nginx 服務,請按以下步驟操作:確定安裝類型:Red Hat/CentOS(systemctl status nginx)或 Debian/Ubuntu(service nginx status)停止服務:Red Hat/CentOS(systemctl stop nginx)或 Debian/Ubuntu(service nginx stop)禁用自動啟動(可選):Red Hat/CentOS(systemctl disable nginx)或 Debian/Ubuntu(syst

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

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


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

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

SublimeText3 Linux新版
SublimeText3 Linux最新版

Atom編輯器mac版下載
最受歡迎的的開源編輯器

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

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器