Nginx學習記錄-3 配置文件Nginx.conf《Nginx高性能Web服務器詳解》筆記

參考php

《Nginx高性能Web服務器詳解》html


第二章:配置文件介紹,基礎配置指令介紹。前端

第三章:模塊化架構介紹。nginx

第四章:比較高級的配置指令web

第五章:Gzip壓縮功能的配置shell

第六章:Rewrite功能的配置瀏覽器

第七章:正向代理、反向代理、負載均衡的配置緩存

第八章:Web緩存功能的配置服務器

第九章:郵件服務功能配置。網絡


配置文件語法

1)Nginx配置文件中,每條指令配置都必須以分號結束。

2)「#」後邊的內容是註釋。

3)分塊配置,常見的塊由http塊、server塊、location塊、upstream塊、mail塊等。一個塊表明一個做用域,做用域存在嵌套。


默認配置文件

### 全局塊 開始 ###

#配置運行Nginx服務器用戶和用戶組
#將此指令行註釋掉,則全部用戶均可以啓動Nginx進程
#user  nobody;

#容許生成的worker process數
worker_processes  1;

#配置錯誤日誌的存放路徑,及日誌的級別(可選)
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#Nginx進程PID存放路徑
#pid        logs/nginx.pid;

### 全局塊 結束 ###

### events塊 開始 ###

events {
#配置最大鏈接數(設置容許每一個worker process同時開啓的最大鏈接數(包括全部可能的鏈接數))
   worker_connections  1024;
}

### events塊 結束 ###

### http塊 開始 ###
http {
#引入配置文件,此處使用的是相對路徑。mime.types用於識別前端請求的資源類型。
   include       mime.types;
#用於處理前端請求的MIME類型
   default_type  application/octet-stream;

#定義服務日誌的格式,以及格式字符串的名字
   #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  logs/access.log  main;

#容許sendfile方式傳輸文件
   sendfile        on;
   #tcp_nopush     on;

#鏈接超時時間
   #keepalive_timeout  0;
   keepalive_timeout  65;

   #gzip  on;

### server塊 開始 ###
#配置虛擬主機 localhost
   server {
#配置監聽端口和主機名稱
       listen       80;
       server_name  localhost;

       #charset koi8-r;

       #access_log  logs/host.access.log  main;
       
#配置處理 / 請求的location
location / {
#配置請求的根目錄
           root   html;
#設置網站的默認首頁。
           index  index.html index.htm;
       }

       #error_page  404              /404.html;

       # redirect server error pages to the static page /50x.html
       #
#設置網站的錯誤頁面
#錯誤代碼 錯誤頁面的路徑(Nginx安裝目錄下的html目錄爲根路徑的相對路徑)或網站的地址
       error_page   500 502 503 504  /50x.html;
#「=」表示要嚴格匹配
       location = /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;
       #}

       # 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 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;
   #    }
   #}

}
### http塊 結束 ###


修改配置文件後,平滑重啓使配置文件生效

sudo ./sbin/nginx -s reload


例:配置一個虛擬主機

1)在nginx.conf中的http塊中加入一下代碼

server {
listen 8082;
#基於IP的虛擬主機配置
server_name 192.168.1.31;

#服務日誌存放目錄,目錄不以「/」開始,它是一個相對路徑,nginx安裝目錄爲根目錄。
access_log myweb/server2/log/access.log;

error_page 404 /404.html;     #設置網站錯誤頁面(404表明沒法找到網頁)

#配置處理/server2/location1請求的location
location /server2/location1 {
root myweb;    #請求的根目錄
index svr2-loc1.html;    #網站的默認首頁
}

location /svr2/loc2 {
alias myweb/server2/location2;    #對location的URI進行更改,後面的是修改後的根路徑
index svr2-loc2.html;
}
location = /404.html {    #配置錯誤頁面轉向
root myweb;
index 404.html;
}
}

2)用ifconfig工具爲同一塊網卡添加一個IP別名。

    1.ifconfig:打印網絡配置信息(個人機器上,使用中的網卡名爲eth0)

    2.爲eth0添加一個IP別名192.168.1.31,用於Nginx服務器提供的虛擬主機。(up表示當即啓用此別名)

ifconfig eth0:0 192.168.1.31 netmask 255.255.255.0 up

    3.使用ifconfig查看是否配置正確。

3)在nginx安裝目錄下新建一個myweb文件夾,安書上的目錄結構放置網頁。

4)在瀏覽器中測試

相關文章
相關標籤/搜索