2018-03-13 Nginx從入門到實踐-基礎篇

一.學習環(huán)境:系統(tǒng)硬件:CPU > =2Core,內(nèi)存 >=256M

操作系統(tǒng):版本>=7.0,位數(shù) X64

二.環(huán)境調(diào)試確認

1,確認系統(tǒng)網(wǎng)絡

2,確認yum可用

yum list | grep gcc

兩項安裝

yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake

yum -y install wget httpd-tools vim

一次初始化

cd /opt;mkdir app download logs work backup

3,確認關(guān)閉iptables規(guī)則

service firewalld stop

ps -ef | grep firewall

4,確認停用seinux




Nginx的中間件架構(gòu)


Nginx的中間件架構(gòu)??

? ? ?一.Nginx的簡述:Nginx是一個開源且高性能,可靠的Http中間件,代理服務.

? ? ?二.常見的Http服務:httpd-Apache基金會,IIS-微軟,GWS-Google

? ? ?三.問:為什么選擇Nginx??

? ? 原因一,IO多路復用epoll多個描述符的I/O操作? ? ? ? 都能在一個線程內(nèi)并發(fā)? ? ? 交替地順序完成,這就叫I/O多路復用,這里的附庸指的是復用同一個線程.什? ? ? ?么是epoll IO多路復用實現(xiàn)方式select,poll,epoll



IO多路復用epoll

select缺點:1.能夠監(jiān)視文件描述符的數(shù)量存在最大限制

? ? ? ? ? ? ? ? ? ?2.線形掃描效率低下

epoll模型:1.每當FD就緒,采用系統(tǒng)的回調(diào)函數(shù)之間將fd放入,效率更高.

? ? ? ? ? ? ? ? ?2.最大連接無限制

原因二,輕量級 功能模塊少 代碼模塊化

原因三,CPU親和(affinity)一,為什么需要CPU親和



Cpu親和(affinity)

什么是Cpu親和:是一種把Cpu親和和Nginx工作進程綁定方式,把每個worker進程固定在一個cpu上執(zhí)行,減少切換cpu的cache miss,獲得更好的性能.

原因四,sendfile



Sendfile


Sendfile

一,Nginx快速搭建與基本參數(shù)使用

Mainline version - 開發(fā)版? ?Stable version - 穩(wěn)定版本 Legacy version - 歷史版本

二,基本參數(shù)使用

安裝目錄 編譯參數(shù) nginx基本配置語法

1,安裝目錄講解

cd /etc/yum.repos.d/

vim nginx.repo

yum -y install nginx

nginx -V

yum list | grep nginx

命令: rpm -ql nginx

/etc/logrotate.d/nginx 配置文件

/etc/nginx 目錄

/etc/nginx/nginx.conf 配置文件

/etc/nginx/conf.d? 目錄

/etc/nginx/conf.d/default.conf 默認配置目錄

/etc/nginx/fastcgi_params fastcgi配置文件

/etc/nginx/uwsgi_params uwsgi配置文件

/etc/nginx/scgi_params scgi配置文件

/etc/nginx/koi-utf 編碼轉(zhuǎn)換影射轉(zhuǎn)化

/etc/nginx/koi-win 編碼轉(zhuǎn)換影射轉(zhuǎn)化

/etc/nginx/win-utf? 編碼轉(zhuǎn)換影射轉(zhuǎn)化

/etc/nginx/mime.types 設置http協(xié)議的Content-Type與擴展名對應關(guān)系

/usr/lib/sysemd/system/nginx-debug.service?

/usr/lib/sysemd/system/nginx.service

/etc/sysconfig/nginx

/etc/sysconfig/nginx

用于配置出系統(tǒng)守護進程管理器管理方式

/usr/lib64/nginx/modules

/etc/nginx/modules

Nginx模塊目錄

/usr/sbin/nginx

/usr/sbin/nginx-debug

Nginx服務的啟動管理的終端命令

/usr/share/doc/nginx-1.12.0

/usr/share/doc/nginx-1.12.0/COPYRIGHT?

/usr/share/man/man8/nginx.8.gz?

Nginx的手冊和幫助文件

/var/cache/nginx

Nginx的緩存目錄

/var/log/nginx

Nginx的日志目錄

2,安裝編譯參數(shù)

命令:nginx-V

--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock

安裝目的的目錄或路徑

--http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp

執(zhí)行模塊時,Nginx所保留的臨時性文件

--user=nginx --group=nginx 設定Nginx進程啟動的用戶和組的用戶

--with-cc-opt=parameters 設置額外的參數(shù)被添加到CFLAGS變量

--with-ld-opt=parameters 設置附加的參數(shù),鏈接系統(tǒng)庫?

Http請求


Http請求

request - 包括請求行,請求頭部,請求數(shù)據(jù)

> GET / HTTP/1.1

> User-Agent: curl/7.29.0

> Host: www.imooc.com

> Accept: */*

response - 包括狀態(tài)行,消息報頭,響應正文

curl -v http://www.imooc.com >/del/null 類似于瀏覽器查看web請求

2,Nginx日志類型

包括:error.log access_log

log_format

Syntax:log_format name [escape=default|json]string...;

Default:log_format combined "...",

Context:http

2.Nginx的變量

Http請求變量 -arg_PARAMRTER,Http_HEADER,sent_http_HEADER

內(nèi)置變量 - Nginx內(nèi)置的

自定義變量 - 自己定義

Nginx模塊講解

Nginx官方模塊

第三方模塊

--with-http_stub_status_module Nginx的客戶端狀態(tài)

--with-http_stub_status_module配置

Syntax:stub_status;

Default:--

Context:server,location

四,默認模塊講解

--with-http_random_index_module 目錄中選擇一個隨機主頁

2,rangdom_index_module

syntax:random_index on | off;

Default:random_index off;

Context:location

--with-http_sub_module HTTP內(nèi)容替換

Syntax : sub_filter string replacement;

Default:--

Context:http,server,location

2.http_sub_module

Syntax: sub_filter_last_modified on|off;

Default:sub_filter_last_modifiied off;

Context:http,server,location

2.http_sub_module

Syntax: sub_filter_once on|off;

Default:sub_filter_once on;

Context:http,server,location

八,Nginx的請求限制

連接頻率限制 - limit_conn_module

請求頻率限制- limit_req_module




Http協(xié)議的連接與請求

Http 1.0 Tcp不能服用

Http 1.1順序性Tcp復用

Http2.0多路復用Tcp服用

八,Http協(xié)議的連接與請求

Http請求建立在一次Tcp連接基礎上

一次Tcp請求至少產(chǎn)生一次Http請求

連接限制

syntax:limit_conn_zone key zone=name:size;

Default:--

context:http

Syntax:limint_conn zone number;

Default:--

Context:http,server,location

八 請求限制

Syntax:limit_req_zone key zone = name:size rate=rate;

Default:--

Context:http

Syntax:limit_req zone=name[burst=number][nodelay];

Default:--

Context:http,server,location

九Nginx的訪問控制

基于IP的訪問控制 http_access_module

Syntax:allow address | CIDR |unix:|all;

Default:--

Context:http,server,location,limit_except

Syntax:deny address | CIDR |unix:|all;

Default:--

Context:http,server,location,limit_except

基于用戶的信任登陸-http_auth_basic_module

http_access_module局限性


http_access_module局限性? 只能通過$remote_addr控制信任


3.http_x_forwarded_for

http_x_forwarded_for=Client IP,Proxy(1)IP,Proxy(2)IP,....

方法一,采用別的Http頭信息控制訪問,如:Http_X_Forward_For

方法二,結(jié)合geo模塊作

方法三,通過http自定義變量傳遞

http_auth_basic_module

Syntax:auth_basic strint | off;

Default:auth_basic off;

Context:http,server,location,limit_except

Syntax:auth_basic_user_file file;

Default:----

Context:http,server,location,limit_except

http_auth_basic_module局限性

一,用戶信息以來文件方式

二,操作管理機械,效率低下

3,解決方案

一,Nginx結(jié)合Lua實現(xiàn)高效驗證

二,Nginx和LDAP打通,利用nginx-auth-ldap

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.yum -y install gcc gcc-c++ autoconf pcre pcre-devel mak...
    銭兎孑閱讀 850評論 0 3
  • 1.簡介: ? Nginx:engine X ,2002年,開源,商業(yè)版? http協(xié)議:web服務器(類似于ht...
    尛尛大尹閱讀 2,021評論 0 3
  • Page 1:nginx 服務器安裝及配置文件詳解 CentOS 6.2 x86_64 安裝 nginx 1.1 ...
    xiaojianxu閱讀 8,695評論 1 41
  • 1.ngnix介紹 ngnix www服務軟件 俄羅斯人開發(fā) 開源 性能很高 本身是一款靜態(tài)WWW軟件 靜態(tài)小文件...
    逗比punk閱讀 2,257評論 1 6
  • 《老男孩Linux運維》筆記 隱藏Nginx軟件版本號 一般來說,軟件的漏洞都和版本有關(guān)。因此要盡量隱藏對訪問用戶...
    Zhang21閱讀 3,906評論 0 28

友情鏈接更多精彩內(nèi)容