nginx常見配置詳解

一.nginx的配置文件javascript

配置文件默認爲安裝目錄下的conf/nginx.conf,若是有使用到其餘子配置文件,能夠在nginx.conf中使用include 文件路徑;的方式加載使用,好比server段,就能夠單獨寫成一個配置文件,在http段下面使用include加載使用。php

nginx.conf配置的結構
...
全局配置區域
...

http {

   ... 
   http段配置
   ...
  server  {
    ......
    ......
    location {
      ...
      ...
    
    }
  }

   server {
      ......
   }
}

 

二.nginx.conf配置文件的常見配置參數css

# nginx的工做進程運行時的身份,也就是進程文件的屬主和屬組屬性,若是在源碼安裝時configure配置已經指定用戶和組,這裏能夠註釋掉
#user  nobody;
# 定義nginx的工做進程的數量,通常爲CPU核數或核數的倍數,該參數與併發量有關
worker_processes  1;
# 錯誤日誌的位置
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# nginx的master進程的pid存儲文件,默認爲/usr/local/nginx/logs/nginx.pid
#pid        logs/nginx.pid;


events {
    # 每個工做進程能夠接收的請求鏈接數,通常與系統的進程能夠打開的文件描述符數量相同,
    worker_connections  1024;    # 併發量=工做進程數量*鏈接數
}


http {
    # mime.types文件含有nginx支持的媒體類型,include能夠加載該文件
    include       mime.types;
    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"';

    # 指定訪問日誌的位置和格式main
    #access_log  logs/access.log  main;
    # 自定義日誌格式,名稱爲shoplog,便於後續使用
    log_format shoplog '$remote_addr@$time_local@$status@$http_user_agent';

    # 調用系統的方法傳輸文件,速度更快,
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;    # 客戶端鏈接的超市時間,單位爲秒,65s後自動斷開
    
    # 隱藏nginx版本號,再也不瀏覽顯示
    server_tokens off;

    #gzip  on;
    server {
        listen 80;
        server_name 192.168.211.109;
        # 根目錄,瀏覽器訪問時默認在該目錄下尋找資源文件
        root html/web;
        
        # 開啓列表展現功能
        #autoindex on;
        # 人性化方式顯示文件的大小
        #autoindex_exact_size off;
        # 顯示本地時間
        #autoindex_localtime on;
        
        # fancy-index第三方模塊提供的功能,使文件列表展現的更優美
        #fancyindex on;
        #fancyindex_exact_size off;        # 人性化方式顯示文件的大小
        
        location / {
            # 執行默認尋找index.html
            index index.html;
            # 反向代理,將請求交給http://192.168.211.101:80服務器處理
        #    proxy_pass http://192.168.211.101:80;
        }
        #location ~* /1.HTML {
        #    error_page 404 = @html_err;
        #}
        #location @html_err {
        #    return 501;
        #}
    } 
    
    server {
        listen 80;
        server_name score.devops.com;
        # 域名重定向
        rewrite / http://shop.devops.com permanent;
    }
    server {
        listen 80;
        server_name shop.devops.com;
        root html/tp5shop/public;
        
        # gzip on壓縮功能,將服務器傳輸的文件壓縮返回給瀏覽器,能夠減小傳輸的數據量,提供性能,通常瀏覽器是支持解壓的
        gzip on;    #開啓壓縮功能
        gzip_http_version 1.0;    # 指定http的版本
        gzip_disable 'MSIE [1-6]';    # 禁止IE的1~6版本使用該功能
        gzip_types application/javascript text/css image/jpeg image/png;    # 指定壓縮哪些類型的文件
        
        # 禁止ip訪問,當有匹配時,就不會在向下匹配
        # deny all;        # 拒絕全部
        # allow 192.168.211.1;         # 容許192.168.211.1
        
        # 用戶訪問限制
        # auth_basic 'pls login:';        # 指定提示語"pls login:"
        # auth_basic_user_file /usr/local/nginx/conf/userlist;        # 指定受權用戶所在文件
        
        # 基於域名的日誌分割,全部訪問shop.devops.com域名的訪問日誌記錄在該文件中
        access_log /usr/local/nginx/logs/shop.devops.com shoplog;
        
        
        location / {
            # expires 設置客戶端緩存
            #expires 1h;
            index index.php index.html;
            
            # 資源重定向,如訪問http://shop.devops.com/index.html後會被重寫爲訪問http://shop.devops.com/index.php,permanent表示永久重定向
            rewrite /index.html /index.php permanent;
            
            # 資源重定向,$request_filename爲nginx的內置變量,表示資源文件路徑
            if (!-e $request_filename) {
                rewrite ^(.*)$ /index.php?s=/$1 last;
                break;
            }
        }
        # 資源重定向
        #location /index {
        #    rewrite /index.html /index.php last;
        #}
        location ~ \.(js|css|jpg|png) {
            # 告訴客戶端全部js,css,jpg,png文件均可以緩存1小時,不用從新在服務器下載
            expires 1h;
            # 防盜鏈實現,全部不是從shop.devops.com跳轉過去訪問js|css|jpg|png文件的都被攔截,返回404
            valid_referers shop.devops.com;
            if ($invalid_referer) {
                return 404;
            } 
        }
        # php解析
        location ~ \.php$ {
        #    root           html;
            fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }


}
相關文章
相關標籤/搜索