nginx常見配置詳解


一.nginx的配置文件

配置文件默認為安裝目錄下的conf/nginx.conf,如果有使用到其他子配置文件,可以在nginx.conf中使用include 文件路徑;的方式加載使用,比如server段,就可以單獨寫成一個配置文件,在http段下面使用include加載使用。

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

http {

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

   server {
      ......
   }
}

 

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

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


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM