CentOS7 源碼安裝Nginx及Nginx基本管理設置


CentOS7 安裝 參考文檔

CentOS7最小安裝后初始化安裝工具

  1:yum install net-tools 參考文檔

     2:源碼安裝wget 參考文檔 或者執行 yum -y install wget

 

CentOS7 源碼安裝包步驟

  一:Nginx 安裝步驟

    1-1:源碼下載地址 http://nginx.org/en/download.html

      執行命令:wget wget http://nginx.org/download/nginx-1.21.3.tar.gz

    1-2:解壓下載的文件 

      執行命令:tar  -zxvf   {壓縮文件名}.tar.gz

    1-3:配置configure 檢測安裝及依賴信息

      查看命令文檔執行:./configure --help

      

 

       先安裝編譯器

      執行:sudo yum -y install gcc

      再次執行:./configure 進行配置 如下圖:

      

 

       檢測到有依賴包沒有裝,根據提示去安裝 pcre

      執行命令:sudo yum -y install pcre-devel

      再次執行:./configure  如下圖:

      

 

      根據提示繼續安裝依賴包:

     執行命令:sudo yum -y install zlib-devel

     一直重復執行./configure 直到檢測到沒有依賴項就說明檢測通過了,如下圖:

     

 

      檢測並指定安裝路徑命令:./configure --prefix=/usr/local/nginx

、    

 

     1-4:執行make指令:源碼生成可執行文件,中途不能出現error

      

 

 

    1-5:安裝 make install

      執行安裝命令:sudo make install

    

   測試:

     1:進入:/usr/local/nginx 執行:./sbin/nginx

     2:在瀏覽器中輸入:http://localhost

     3:安裝一個文本瀏覽器測試 yum -y install elinks

      

   

  Nginx相關目錄文件

    nginx path prefix:/usr/local/nginx (Nginx安裝目錄)

    nginx binary file:/usr/local/nginx/sbin/nginx(Nginx啟動目錄)

    nginx moduless path:/usr/local/nginx/modules(Nginx相關模塊目錄)

    nginx configuration prefix:/usr/local/nginx/conf(Nginx配置文件目錄) 

    nginx configuration file:/usr/local/nginx/conf/nginx.con(nginx啟動配置文件)

    nginx pid file:/usr/local/nginx/logs/nginx.pid(nginx進程號)

    nginx error log file:/usr/local/nginx/logs/error.log:(nginx錯誤日志文件)

    nginx http access log file:/usr/local/nginx/logs/access.log(訪問日志文件)

   

  先關閉防火牆 (測試需要在局域網中訪問)

    setenforce 0
    systemctl stop firewalld
    systemctl disable firewalld

 

     Nginx添加到環境變量

    使用軟連接將nginx鏈接到/usr/local/sbin

    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin || /usr/local/sbin/ | grep "nginx" 

               

    顯示當前環境變量PATH

      echo $PATH

    編輯.bash_profile文件

      vim ~/.bash_profile

    在.bash_profile文件末尾加入以下內容

      export PATH=$PATH:/usr/local/nginx/sbin

    

    引用.bash_profile文件

      source ~/.bash_profile

    使用nginx命令

    # 啟動nginx

      nginx

    # 停止nginx

      nginx -s quit

  

nginx部分配置

# 啟動該程序的默認程序
#user  nobody;
# 一個主進程和多個工作進程。這里定義的是主進程數量
worker_processes  4;

# 全局錯誤日志的位置及日志格式
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    # 每個工作進程最大並發數
    worker_connections  1024;
}

# http 服務設置
http {
    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"';

    # access_log  logs/access.log  main; # 全局日志路徑
    # $remote_addr與$http_x_forwarded_for用以記錄客戶端ip地址
    # $remote_user:記錄客戶端名稱
    # $time_local:記錄訪問時間與時區
    # $request:記錄訪問狀態 200成功
    # $body_bytes_sent:記錄發送給客戶端文件主體內容大小
    # $http_referer:記錄從哪個頁面鏈接訪問過來的
    # $http_user_agent:記錄客戶端瀏覽器相關信息

    # sendfie指令指定nginx是否調用sendfile函數(zero copy 方式)來輸出文件
    sendfile        on;
    # 允許或禁止使用socke的TCP_CORK的選項僅在使用sendfile的時候使用
    #tcp_nopush     on;
    # 長連接超時時間
    #keepalive_timeout  0;
    keepalive_timeout  65;
    # 開啟壓縮
    #gzip  on;
    # 默認網站  配置虛擬主機
    server {
    # 虛擬主機使用的端口
        listen       80;
    # 主機域名
        server_name  localhost;
    # 支持的字符集
        charset utf-8;
        #訪問日志路徑
        #access_log  logs/host.access.log  main;
    # 定義web根路徑
        location / {
        # 根目錄路徑
            root   html;
        # 索引頁面
            index  index.html index.htm;
        }

    # 訪問控制目錄
    location /a {
        # 允許訪問
        allow  127.0.0.0;
        # 不允許
        deny  all;
        # return 404;
        return  http://www.baidu.com;
    }    
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        # 根據錯誤碼返回對應的頁面
        error_page   500 502 503 504  /50x.html;
        # 
    location = /50x.html {
            root   html;
        }

    }
}
View Code

  

  如果修改了配置文件先進入nginx目錄中:cd /usr/local/nginx ,在執行:./sbin/nginx -g ./conf/nginx.conf 檢測配置文件是否正確

  修改配置重啟nginx 方式一:

    先執行:killall nginx

    在執行:cd /usr/local/nginx 執行:./sbin/nginx

  修改配置重啟nginx 方式一:

    killall -s HUP nginx (重新加載配置文件)

  nginx訪問目錄配置

    注意:訪問控制目錄中的location /a{} 這里的location值得就是 web根路徑中的root路徑

    # 定義web根路徑
        location / {
        # 根目錄路徑
            root   html;
        # 索引頁面
            index  index.html index.htm;
        }

    # 訪問控制目錄
    location /a {
        # 允許訪問
        allow  127.0.0.0;
        # 不允許
        deny  all;
        # return 404;
        return  http://www.baidu.com;
    }    

 Nginx日志管理

   Nginx訪問日志主要有兩個參數控制

    1:log_format 用來定義日志格式,可以定義多種日志格式,取不同名字即可

    2:access_log 用來指定日志文件路徑及使用何種日志格式記錄日志

   

  log_format格式變量:

    # access_log logs/access.log main; # 全局日志路徑

    # $remote_addr與$http_x_forwarded_for用以記錄客戶端ip地址

  `   # $remote_user:記錄客戶端名稱
    # $time_local:記錄訪問時間與時區
    # $request:記錄訪問狀態 200成功
    # $body_bytes_sent:記錄發送給客戶端文件主體內容大小
    # $http_referer:記錄從哪個頁面鏈接訪問過來的
    # $http_user_agent:記錄客戶端瀏覽器相關信息

   

 

   自定義格式為json格式:  

   # 自定義日志格式
    # log_format default_fmt '[$time_local] $remote_addr "$request"'
    # 自定義json日志格式
    log_format default_fmt_json '{"@timestamp":"$time_local",'
                                '"client_ip":"$remote_addr",'
                                '"request":"$request",'
                                '"status":"$status",'
                                '"bytes":"$body_bytes_sent",'
                                '"x_forwarded":"$http_x_forwarded_for",'
                                '"referer":"$http_referer"'
                                '}';

  兩種格式測試效果如下:

    

 

Nginx圖片防盜鏈

   

        # 防盜鏈(只針對b目錄)
        # location /b {
        # 項目中以下文件后綴
        location ~* \.(png|gif|bmp|jpg|jpeg)$ {
        valid_referers none blocked *.ticp.net;
        if ($invalid_referer){
                return 403;
        }
        }

 

Nginx虛擬主機

    一個web服務器軟件默認情況下只能發布一個web,因為一個web分享出去需要三個條件(IP、Port、Domain name)

    虛擬主機就是把一台物理服務器划分成多個虛擬服務器,每個虛擬主機都可以有獨立的域名和獨立的目錄。

    

 

 

  1:基於IP的虛擬主機

    准備:

      1:在nginx根目錄下准備兩個網站如:

        /usr/local/nginx/html/web1/index.html

        /usr/local/nginx/html/web1/index.html

      2:添加一個子IP,使用邏輯網卡添加一個 自網卡的方式

        添加網卡:ifconfig ens33:1 192.168.0.131/24 up

        刪除網卡:ifconfig ens33:1 down

      

 

   基於IP虛擬主機nginx配置信息 (/usr/local/nginx/conf/nginx.conf)

    

user centos;
worker_processes  4;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    
    log_format default_fmt_json '{"@timestamp":"$time_local",'
                    '"client_ip":"$remote_addr",'
                '"request":"$request",'
                '"status":"$status",'
                '"bytes":"$body_bytes_sent",'
                '"x_forwarded":"$http_x_forwarded_for",'
                '"referer":"$http_referer"'
                '}';    
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       192.168.0.130:80;
        server_name  localhost;
        charset utf-8;
        access_log  logs/host.access.log  default_fmt_json;
        location / {
            root   html/web1;
            index  index.html index.htm;
        }
    }
    server {
        listen       192.168.0.131:80;
        server_name  localhost;
        charset utf-8;
        access_log  logs/host.access.log  default_fmt_json;
        location / {
            root   html/web2;
            index  index.html index.htm;
        }
    }
}

  重啟nginx 測試 (先:killall nginx 在:../sbin/nginx )

   

 

 

  

 

   2:基於端口的虛擬主機(只需要修改配置即可)

user centos;
worker_processes  4;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    
    log_format default_fmt_json '{"@timestamp":"$time_local",'
                    '"client_ip":"$remote_addr",'
                '"request":"$request",'
                '"status":"$status",'
                '"bytes":"$body_bytes_sent",'
                '"x_forwarded":"$http_x_forwarded_for",'
                '"referer":"$http_referer"'
                '}';    
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        #server_name  localhost;
        charset utf-8;
        access_log  logs/host.access.log  default_fmt_json;
        location / {
            root   html/web1;
            index  index.html index.htm;
        }
    }
    server {
        listen       8080;
        #server_name  localhost;
        charset utf-8;
        access_log  logs/host.access.log  default_fmt_json;
        location / {
            root   html/web2;
            index  index.html index.htm;
        }
    }
}
View Code

    

 

     測試

    

 

   3:基於域名的虛擬主機配置文件

    

 

Nginx反向代理

    代理服務器,客戶在發送請求的時候,不會直接發送給目的主機,而是先發送給代理服務器,代理服務器接受客戶機請求之后,再向主機發出,並接收目的主機返回的數據,存放在代理服務器硬盤中,在發送給客戶機

    

 

 

    應用場景:

        堡壘機場景

        內網服務器發布場景

        緩存場景

        

 

 

        

    

    代理服務器原理:

           

 

     代理配置文件  

user centos;
worker_processes  4;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    
    log_format default_fmt_json '{"@timestamp":"$time_local",'
                    '"client_ip":"$remote_addr",'
                '"request":"$request",'
                '"status":"$status",'
                '"bytes":"$body_bytes_sent",'
                '"x_forwarded":"$http_x_forwarded_for",'
                '"referer":"$http_referer"'
                '}';    
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        #server_name  localhost;
        charset utf-8;
        access_log  logs/host.access.log  default_fmt_json;
        location / {
            proxy_pass http://wendj.ticp.net;
        }
    }
}
View Code

    

Nginx限速

      Nginx官方版本限制IP的鏈接和並發分別有兩個模塊

        limit_req_zone:用來限制單位時間內的請求數,即速率限制。

        limit_req_conn:用來限制同一時間連接數,即並發限制。

         

 

   Nginx中URL重寫

      rewrite模塊(ngx_http_rewrite_module)

      Rewrite功能是Nginx服務器提供的一個重要功能。幾乎所有的web產品必備技能,用於實現URL重寫。URL重寫是非常常用的功能,比如它可以在我們改變網站結構后,不需要客戶端修改原來的書簽,也不需要其它網站吸怪對我網站的友情鏈接,還可以在一定程度上提高網站安全性。

      Nginx服務器Rewrite功能是依賴於PCRE(PerlCompatibleRegularExpression)

      

Nginx優化

    Nginx是主進程+工作進程模型

    worker_processes 1:工作進程數量,按CPU的總核心調整

    worker_cpu_affinity 0001 0100 1000:CPU的親和力

    worker_connections 1024:一個工作進程的並發數

  查看CPU核數指令:cat /proc/cpuinfo |grep "flags"|wc -l

    

 

   統計nginx連接數:netstat -antpl|grep nginx|grep ESTABLISHED|wc -l

   

  Nginx長連接

     http協議屬於TCP協議

     優化目標:減少三次握手和四次揮手次數

     keepalive_timeout 5:長連接時間

     keepalive_requests 8192:每個長連接接受最大請求數

   Nginx數據壓縮

     gzip on;      # 開啟壓縮

     gzip_proxied any;  # 任何時候都壓縮

     gzip_min_length 1k;  # 啟用壓縮最小文件,小於設置的不會被壓縮

        gzip_buffers 4 8k;  # 壓縮的緩存區大小

     gzip_comp_level 6;  # 壓縮級別1-9數字越大壓縮的越好,也越占CPU時間

     gzip_types text/plain text/css application/x-javascript application/javascript application/xml;  # 壓縮文件類型

   Nginx客戶端緩存

     location ~* \.(png|gif|jpg|jpeg)${

     expires 1h;

     }

  

  Nginx參考文章

 

  


免責聲明!

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



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