nginx1.14的安裝


 

 

編譯安裝nginx1.14.2

 

#拷貝指定文件到當前目錄下
[root@localhost ~]# find /usr/share -iname "*.jpg" -exec cp {} ./ \;

 

 

1、運行環境包

yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel lua-devel perl


yum groupinstall "Development Tools" "Server Platform Development"

 

 

2、額外的軟件包

LuaJit: http://luajit.org/download.html
ningx:  https://nginx.org/download/nginx-1.14.2.tar.gz
pcre:    https://ftp.pcre.org/pub/pcre/pcre-8.42.zip
ngx_devel_kit:    https://github.com/simplresty/ngx_devel_kit/tags  
 
lua-nginx-module    https://github.com/openresty/lua-nginx-module/tags
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.14rc3.tar.gz --no-check-certificate

wget http://ftp.17gogoing.com:8852/pub/nginx/nginx_upstream_check_module-master.tar.gz   —— 檢查后端服務器的狀態
nginx_goodies: https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/downloads/?tab=downloads   下載地址 —— 后端做負載均衡解決session sticky問題

注釋:所有的軟件包均下載到/usr/local/src目錄下

百度雲鏈接地址:

 

3、安裝編譯

cd /usr/local/src/nginx-1.14.2

patch -p1 < /usr/local/src/nginx_upstream_check_module-master/check_1.12.1+.patch

#編譯參數如下: .
/configure \ --user=nginx \ --group=nginx \ --prefix=/usr/local/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --pid-path=/var/log/nginx/nginx.pid \ --lock-path=/usr/local/nginx/lock/nginx.lock \ --http-log-path=/var/log/nginx/access.log \ --with-openssl=/usr/local/src/openssl-1.0.2k \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_mp4_module \ --http-proxy-temp-path=/usr/local/nginx/proxy_tmp \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre=/usr/local/src/pcre-8.42 \ --add-module=/usr/local/src/ngx_devel_kit-0.3.0 \ --add-module=/usr/local/src/lua-nginx-module-master \ --add-module=/usr/local/src/nginx_upstream_check_module-master make -j 4 && make install

 

4、其他操作(壓力測試)

1、nginx綁定cpu(純nginx服務時才能進行綁定)

在沒有綁定cpu的情況下,nginx會動態調用cpu,降低緩存命中率;

#動態每5秒刷新一次
[root@localhost nginx]# watch -n .5 "ps axo comm,pid,psr |grep nginx"
nginx            13340   3
nginx            13341   0
nginx            13342   1
nginx            13343   3
nginx            13344   1

#進行壓力測試
[root@localhost ~]# yum -y install httpd-tools
#一次10萬並發,連接100次
[root@localhost ~]# ab -n 100000 -c 100 http://11.11.11.2/index.html

#在鏈接的過程中會發現nginx調用的cpu是不固定的;

如果只綁定3個的話,優先不使用第一個cpu;

 

 

2、指定worker進程的nice值(運行優先級 -20~20之間)

worker_priority -5;

 

 

3、指定某些IP地址不能訪問,其他可以正常訪問

server {
      listen 80;
      server_name www.yanqi.org;
      root /www/nginx/html;
      
      location ~* \.(jpg|png)$ {
               deny 11.11.11.3;
               allow all;
}
}

 

location / {
    satisfy any;

    allow 192.168.1.0/32;
    deny  all;

    auth_basic           "closed site";
    auth_basic_user_file conf/htpasswd;
}

 

4、location配置說明

4.1、配置文件位置說明(優選級說明)

 

4.2、配置路徑說明(定義文件位置 根/別名

 

 

 

5、定義錯誤頁面

server {
        listen 80;
        server_name www.yanqi.org;
        root /data/nginx/vhost;

        location / {
             #root /data/nginx/vhost2;
             allow all;
        }

        error_page 404 = 200 /404.html
         location = /404.html {
             root /data/nginx/error_pages;
         }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
}

說明:
如果訪問404頁面:會到
/data/nginx/error_pages 目錄下找 404.html 文件; 注釋:=200 是定義響應碼,就算是訪問是吧,返回的狀態碼也是200
如果訪問50x頁面:會到 /data/nginx/vhost 目錄下找50x.html;

 

6、定義客戶端請求的相關配置

只要記住前兩個的使用即可

 

 7、對客戶端進行限制的相關配置

 

 8、文件操作優化配置

 

 9、模塊的說明介紹

9.1、ngx_http_access_module (基於IP地址的訪問控制模塊)

 http://nginx.org/en/docs/http/ngx_http_access_module.html

location / {
    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
}

 

 

 9.2、ngx_http_auth_basic_module(網頁密碼登陸驗證)

http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html

location / {
    auth_basic           "closed site";
    auth_basic_user_file conf/htpasswd;
}

 

 

 

 

9.3、nginx連接狀態

 

 9.4、nginx的訪問日志

http://nginx.org/en/docs/http/ngx_http_log_module.html

 

 

 9.5、傳輸壓縮

http://nginx.org/en/docs/http/ngx_http_gzip_module.html

gzip on | off;    開啟壓縮
gzip_comp_level 6;  壓縮級別
gzip_buffers 32 4k|16 8k;  壓縮緩沖  16段 每段8k   默認值
gzip_min_length 20;   低於20字節的內容不壓縮   默認值
gzip_types text/html text/css application/javascript;   指定壓縮目標  #這個可以在/etc/nginx/mime.types 中查看

前段有代理不開啟,默認配置

 

 

 

 

9.5、 ngx_http_ssl_module(http server)

http://nginx.org/en/docs/http/ngx_http_ssl_module.html

http {

    ...

    server {
        listen              443 ssl;
        keepalive_timeout   70;

        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers         AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
        ssl_certificate     /usr/local/nginx/conf/cert.pem;
        ssl_certificate_key /usr/local/nginx/conf/cert.key;
        ssl_session_cache   shared:SSL:10m;
        ssl_session_timeout 10m;

        ...
    }

 

9.6、ngx_http_rewrite_module(重定向)

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

問題:

http://www.yanqi.org/bbs/    --> http://bbs.yanqi.org
http://www.yanqi.org/media/audio/a.wmv  --> http://www.yanqi.org/media/mp3/a.mp3

 

 

1、只要訪問資源為.png結尾的,全部定義成.jpg

注釋:圖片資源之前是.png文件格式,后來全部轉換成了.jpg;返還的狀態仍是200

server {
listen 80;
server_name www.yanqi.org;
root /data/nginx/vhost1;
rewrite /(.*)\.png$ /$1.jpg;
}

 

 

2、域名跳轉

 

[root@nginx conf.d]# cat vhost1.conf 
server {
  listen 443 ssl;
  server_name cahost.zzidc.com;
  root /data/nginx/vhost1;
  access_log /var/log/nginx/vhost1_ssl_access.log main;

  ssl on;
  ssl_certificate /etc/nginx/ssl/nginx.crt;
  ssl_certificate_key /etc/nginx/ssl/nginx.key;
  ssl_protocols sslv3 TLSv1 TLSv1.1 TLSv1.2;
  ssl_session_cache   shared:SSL:10m;
  ssl_session_timeout 10m;
}

server {
   listen 80;
   server_name www.yanqi.org;
   root /data/nginx/vhost1;
   #rewrite /(.*)\.png$ /$1.jpg;   #只要是以.png結尾的uri統一轉成訪問同名下格式為.jpg
   #rewrite /(.*)$ https://cahost.zzidc.com/$1;   #不管你訪問什么,最終都跳轉到后邊的域名,默認301永久
#rewrite /(.*).png$ /$1.jpg; #服務端自行轉換,返回給用戶的響應碼仍然是200
rewrite /(.*).png$ /$1.jpg redirect; #返回給客戶一個新的URL,客戶端再去訪問新的URL 302 臨時重定向
rewrite /(.*).png$ /$1.jpg redirect; #同樣需要客戶端重新請求, 301 永久重定向
 
        

 注釋:

 

 

 3、if 判斷   

if ($http_user_agent ~ MSIE) {          #如果瀏覽器是IE的話,所有請求跳轉到/msie/下,然后跳出本次循環
    rewrite ^(.*)$ /msie/$1 break;
}

if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
    set $id $1;
}

if ($request_method = POST) {     #如果客戶端請求方法為post的話,直接返回405
    return 405;
}

if ($slow) {
    limit_rate 10k;
}

if ($invalid_referer) {    #防盜鏈,在定義中沒有配置的,直接返回403;這個內容后續有寫明。 return 403;
}

 

 

 

 4、ngx_http_referer_module (防盜鏈)

http://nginx.org/en/docs/http/ngx_http_referer_module.html

 


免責聲明!

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



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