進入nginx目錄,排除temp文件後,剩餘四個目錄html
[root@elkzabbix01 nginx]# ls -l | grep -v templinux
總用量 36nginx
drwxr-xr-x. 2 root root 4096 7月 12 20:42 conf → 配置文件web
drwxr-xr-x. 2 root root 4096 7月 12 20:42 html → 站點信息apache
drwxr-xr-x. 2 root root 4096 7月 12 20:49 logs → 日誌信息windows
drwxr-xr-x. 2 root root 4096 7月 12 20:42 sbin → 啓動命令瀏覽器
cd /html 服務器
裏面有文件index.html ,這個文件通常默認是網站的首頁併發
下面進入conf文件夾,下面有一個 nginx.conf 文件app
運維有一個很重要的思想叫最小化學習,因此咱們要把配置文件簡化一下:
簡化命令:
egrep -v "#|^$" nginx.conf >> a.conf
^表明行首
$表明行尾
^$意思就是行首以後就是行尾,中間什麼也沒有,因此表明空行
worker_processes 1; 默認進程有一個
一個worker能夠處理多少併發鏈接
events {
worker_connections 1024;
}
那麼有多少個worker呢???能夠看配置文件第一行
[root@elkzabbix01 conf]# ps -ef | grep nginx
root 8398 1 0 Jul12 ? 00:00:00 nginx: master process /app/zpy/nginx/sbin/nginx
zpy 8399 8398 0 Jul12 ? 00:00:00 nginx: worker process
能夠看到就一個worker process ,通常認爲worker process 與cpu 核數至關
因此nginx最大併發鏈接數是怎麼計算的呢 ??
worker process 乘以 worker_connections
-----------------------
http模塊
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
下面這個叫作server標籤,一個server標籤表明一個虛擬主機
server {
listen 80; 監聽端口
server_name localhost; 域名
location / { / 表明直接在瀏覽器輸入 http://10.0.70.3
root html; root表明nginx安裝目錄下的html文件夾,好比說你nginx安裝在了/opt/nginx 下,那麼html表明的目錄就爲 /opt/nginx/html,可是若是你寫 root /html ,那就不是相對路徑了(nginx安裝目錄下),那就是/html了
index index.html index.htm; 這個表明首頁文件,就是說nginx下html文件夾下的index.html文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html { /50.x.html表明直接在瀏覽輸http://10.0.70.3/50.x.html
root html;
}
關於root和alias的區別:
乾脆來講說alias標籤和root標籤的區別吧。
最基本的區別:alias指定的目錄是準確的,root是指定目錄的上級目錄,而且該上級目錄要含有location指定名稱的同名目錄。另外,根據前文所述,使用alias標籤的目錄塊中不能使用rewrite的break。
說不明白,看下配置:
location /abc/ {
alias /home/html/abc/;
}
在這段配置下,http://test/abc/a.html就指定的是/home/html/abc/a.html。這段配置亦可改爲
location /abc/ {
root /home/html/;
}
這樣,nginx就會去找/home/html/目錄下的abc目錄了,獲得的結果是相同的。
可是,若是我把alias的配置改爲:
location /abc/ {
alias /home/html/def/;
}
那麼nginx將會從/home/html/def/取數據,這段配置還不能直接使用root配置,若是非要配置,只有在/home/html/下創建一個 def->abc的軟link(快捷方式)了。
通常狀況下,在location /中配置root,在location /other中配置alias是一個好習慣。
nginx 虛擬主機的概念
所謂的虛擬主機,在web服務裏就是一個獨立的網站站點,這個站點對應獨立的域名(也多是IP或者端口),具備獨立程序及資源目錄,能夠獨立的對外提供服務供用戶訪問
apache是如何定義虛擬主機的 ?
配置文件中 <VirtualHost> </VirtualHost> 內
nginx是如何定義虛擬主機的?
在配置文件中server{} 的標籤
虛擬主機通常分爲以下三類:
基於域名的虛擬主機
基於端口的虛擬主機
基於ip的虛擬主機
配置基於域名的虛擬主機
server {
listen 80;
server_name www.vipdailiang.com;
location / {
root html/www;
index www.html ;
}
}
1)首先須要在/app/zpy/nginx/html下建立 www目錄
mkdir -p /app/zpy/nginx/html/www
2)在www目錄下建立www.html,並添加www.zipeiyi.com的內容
echo "www.vipdailiang" >> /app/zpy/nginx/html/www/www.html
3)最重要的一步,nginx一旦應用在企業環境中,它的做用會變得很重要,因此修改了配置文件,不要貿然重啓,須要先檢查一遍nginx配置文件的合理性
/app/zpy/nginx/sbin/nginx -t
nginx: the configuration file /app/zpy/nginx-1.8.1//conf/nginx.conf syntax is ok
nginx: configuration file /app/zpy/nginx-1.8.1//conf/nginx.conf test is successful
4)平滑重啓nginx
/app/zpy/nginx/sbin/nginx -s reload
備註:
若是nginx參數過多記不住怎麼辦??
/app/zpy/nginx/sbin/nginx -h
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /app/zpy/nginx-1.8.1//)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
最後在瀏覽器輸入10.0.70.3
可是若是輸入:www.vipdailiang.com
這是由於你電腦本機解析不到的緣由:
有兩種解析的辦法:
修改本機的host文件(linux是/etc/hosts ,windows的C\windows\system32\drivers\etc\hosts)
添加10.0.70.3 www.vipdailiang.com
在本地DNS裏面加入域名對應關係
最後效果以下,至此基於域名的虛擬主機配置完畢
配置多個基於域名的虛擬主機
server {
listen 80;
server_name www.vipdailiang.com;
location / {
root html/www;
index www.html ;
}
}
server {
listen 80;
server_name bbs.vipdailiang.com;
location / {
root html/bbs;
index bbs.html ;
}
}
這樣你就可能會想了,怎麼都是80端口,那麼我在瀏覽器輸入http://10.0.70.3 會出現什麼???
別急,細細道來
我作了一個實驗,發現是下圖的結構:
若是你用ip進行測試的化,那麼應該是按照 server{}的順序來的
總結:基與域名的虛擬主機,就不要這樣測試了,應該用域名測試
到此基於域名的nginx虛擬主機講解完畢
配置基於端口的虛擬主機
server {
listen 8001;
server_name www.vipdailiang.com;
location / {
root html/www;
index www.html ; → 這裏面內容改成 ism
}
}
server {
listen 8002;
server_name www.vipdailiang.com;
location / {
root html/bbs;
index bbs.html ; →這裏面內容改成imp
}
}
netstat -tunpl | grep nginx ,能夠看到nginx起了8001與8002兩個端口
tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 8398/nginx
tcp 0 0 0.0.0.0:8002 0.0.0.0:* LISTEN 8398/nginx
那麼該怎麼檢測呢??
curl www.vipdailiang.com:8001
ism
curl www.vipdailiang.com:8002
imp
配置基於IP的虛擬主機
這個須要有多塊網卡,nginx服務器須要多個ip
這個在生產環境中極爲少見,這裏就不作解釋了
nginx 開啓目錄瀏覽
今天工做須要,要給客戶提供一個patch的下載地址,因而想用nginx的目錄瀏覽功能來作,須要讓客戶看到指定一個目錄下的文件列表,而後讓他本身來選擇該下載那個文件;
咱們都知道在apache下能夠配置訪問web服務器的某個路徑時,自動顯示其目錄下面的文件列表的,其實Nginx一點也不比apache弱,它固然也能夠實現這個功能,並且還很是容易和簡單;主要用到autoindex 這個參數來開啓,其配置以下:
代碼以下:
location / {
alias /software/tools/; #注意tools後面的/ ,加/ 與不加 這個有區別的
autoindex on; #//開啓目錄瀏覽功能;
autoindex_exact_size off; # //關閉詳細文件大小統計,讓文件大小顯示MB,GB單位,默認爲b;
autoindex_localtime on; #//開啓以服務器本地時區顯示文件修改日期!
}
注意:
ok。。至此nginx配置文件就講解完成了