nginx都是經過配置文件來進行工做的,多數狀況下只須要配置nginx.conf文件,它就能開始工做了。
nginx的核心配置文件nginx.conf主要由三部分組成。以下圖:
nginx默認配置文件內容以下:php
##########如下爲基本配置########### #user nobody; #用於配置worker進程的運行用戶,linux有個默認用戶nobody worker_processes 1; #配置工做進程數目,根據硬件調整,一般等於CPU數量或者2倍於CPU數量 #error_log logs/error.log; #配置全局錯誤日誌以及類型, #日誌級別有:debug|info|notice|warn|error|crit,默認是error級別。 #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; #配置進程pid文件 ############################## ##############如下是事件配置############ events { worker_connections 1024; #配置每一個worker進程鏈接數上限,nginx支持的總鏈接數=進程數*鏈接數,#即worker_processes* worker_connections } ##################################### ############如下是http服務配置######### http { ########如下是http服務配置中的基礎配置,是通用的 include mime.types;#配置nginx支持哪些多媒體類型,能夠在conf/mime.types中查看支持哪些。 default_type application/octet-stream;#默認支持的文件類型,流格式文件。 #配置日誌的輸出格式 $表示nginx本身的變量 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #配置access.log日誌以及存放路徑,而且使用上面main定義的輸出日誌格式 #access_log logs/access.log main; sendfile on;#開啓高效文件傳輸模式 #tcp_nopush on;#防止網絡阻塞 #keepalive_timeout 0; keepalive_timeout 65;#長鏈接超時時間,單位是秒 #gzip on; #開啓gzip壓縮輸出,啓用後nginx會將服務器返回的較大數據進行壓縮後響應到客戶端 #gzip_min_length 1k;#配置最小壓縮文件大小 #gzip_buffers 4 16k;#配置壓縮緩衝區 #gzip_http_version 1.0;#壓縮版本(默認1.1) #gzip_comp_level 2;壓縮等級,數字越大壓縮的越厲害 ######################## ############ 如下是虛擬主機配置,能夠配置多個server,每個server都表示一個獨立的虛擬主機站點 ###配置虛擬主機############# server { listen 80;#配置的監聽端口爲80 server_name localhost;#配置服務名,也就是提供服務的域名主機名 #charset koi8-r; #配置字符集 #access_log logs/host.access.log main;#配置本虛擬機的訪問日誌 #默認的斜槓/的請求,當訪問路徑中有斜槓/時都會被改location匹配到並進行處理 location / { root html;#root是配置服務器的默認網站根目錄位置,默認爲nginx安裝目錄下的html目錄 index index.html index.htm;#配置默認的首頁文件,多個用空格分開 } #error_page 404 /404.html;#配置404頁面 # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html;#配置50x錯誤頁面 location = /50x.html {#=/50x.html是表示精確配置 root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} ####如下配置是進行的禁止訪問配置,如下是禁止訪問.htaccess文件的配置 # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} #######如下是配置https服務,即安全加密的網絡傳輸服務。 # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
配置完成後能夠經過-t命令來進行檢測配置是否正確。html
在Windows平臺下輸入命令:nginx -c -t ./conf/jason.conf,(-c表示配置文件縮寫,-t表示test縮寫)輸出結果以下圖所示:linux
若是配置有錯則會以下提示:nginx
在linux平臺下輸入命令: nginx -c ./conf/nginx.conf -t 。其實和Windows平臺是同樣的。安全
查看nginx版本信息:-v和-V,一個小寫v,一個大寫V,兩個的含義有些不一樣。
nginx -v:只是顯示nginx的當前版本,以下圖服務器
nginx -V:顯示nginx版本、編譯器版本和配置參數信息,以下圖網絡
以上就是nginx配置文件的大概狀況,知道http區塊中能夠配置多個server服務,每個server服務都是一個虛擬主機站點,配置完成後能夠經過-t來進行檢測。session