1. 程式人生 > 實用技巧 >nginx.conf 配置解析之 server配置

nginx.conf 配置解析之 server配置

server{} 包含在http{}內部,每一個server{}都是一個虛擬主機(站點)

以下為nginx.conf配置檔案中server{ }部分的內容。

server {
    listen       80;  //監聽埠為80,可以自定義其他埠,也可以加上IP地址,如,listen 127.0.0.1:8080;
    server_name  localhost; //定義網站域名,可以寫多個,用空格分隔。
    #charset koi8-r; //定義網站的字符集,一般不設定,而是在網頁程式碼中設定。
    #access_log  logs/host.access.log  main; //定義訪問日誌,可以針對每一個server(即每一個站點)設定它們自己的訪問日誌。

    ##在server{}裡有很多location配置段
    location / {
        root   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;  //當狀態碼為500、502、503、504時,則訪問50x.html
    location = /50x.html {
        root   html;  //定義50x.html所在路徑
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #定義訪問php指令碼時,將會執行本location{}部分指令
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;  //proxy_pass後面指定要訪問的url連結,用proxy_pass實現代理。
    #}

    # 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伺服器監聽埠與地址,支援兩種形式,1 IP:Port, 2 unix:/path/to/sockt
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  //定義SCRIPT_FILENAME變數,後面的路徑/scripts為上面的root指定的目錄
    #    include        fastcgi_params; //引用prefix/conf/fastcgi_params檔案,該檔案定義了fastcgi相關的變數
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    # 
    #location ~ /\.ht {   //訪問的url中,以/.ht開頭的,如,www.example.com/.htaccess,會被拒絕,返回403狀態碼。
    #    deny  all;  //這裡的all指的是所有的請求。
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;  //監聽8000埠
#    listen       somename:8080;  //指定ip:port
#    server_name  somename  alias  another.alias;  //指定多個server_name

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443 ssl;  //監聽443埠,即ssl
#    server_name  localhost;

### 以下為ssl相關配置
#    ssl_certificate      cert.pem;    //指定pem檔案路徑
#    ssl_certificate_key  cert.key;  //指定key檔案路徑

#    ssl_session_cache    shared:SSL:1m;  //指定session cache大小
#    ssl_session_timeout  5m;  //指定session超時時間
#    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   //指定ssl協議
#    ssl_ciphers  HIGH:!aNULL:!MD5;  //指定ssl演算法
#    ssl_prefer_server_ciphers  on;  //優先採取伺服器演算法
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}