如果虛擬主機很多的話,進行分離看起來會更方便,還可以按功能、業務進行劃分,下面以兩個虛擬主機為例。
完整的除去空行和註解後的設定檔:
[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
[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
以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
狀態資訊記錄使用的是`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次請求
error_log語法:
error_log file level;
#關鍵字不變,file是日誌存放位置,level是錯誤日誌等級通常只用warn|error|crit三個等級
配置錯誤日誌配置,在
nging.conf
worker_processes 1;下新增
error_loglogs/error_log;
######沒錯,就這一行! ###以上是Nginx安裝後常用功能如何設定的詳細內容。更多資訊請關注PHP中文網其他相關文章!