關於Nginx的基礎內容

來源:互聯網
上載者:User
這篇文章主要介紹了關於Nginx的基礎內容,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

Nginx-基礎篇

一、環境:

  • 系統硬體:CPU>=2Core,記憶體>=256M

  • 作業系統:CentOS 7.2 x64

二、環境調試確認:

1、四個確認

  1. 確認系統網路

    • ping www.baidu.com

  2. 確認yum可用

    • yum list

  3. 確認關閉iptables規則

    • iptables -L(查看是否有iptables規則)

    • iptables -F(關閉規則)

    • iptables -t nat -L(查看net表裡有沒有規則)

    • 如果net表中有規則可以執行:iptables -t nat -F

  4. 確認停用selinux

    • getenforce(查看selinux是否開啟)

    • setenforce 0 (關閉selinux)

2、兩項安裝

  • 安裝gcc等:

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

  • 安裝基本工具:

    • yum -y install wget httpd-tools vim

3、一次初始化

  • cd /opt;mkdir app download logs work backup

    • app:代碼目錄

    • download:網上下載的源碼包

    • logs:自訂日誌

    • work:shell指令碼

    • backup:備份

三、什麼是Nginx:

Nginx是一個開源且高效能、可靠的HTTP中介軟體、代理服務。

四、Nginx優勢:

  1. IO多工epoll

  2. 輕量級

    • 功能模組少

    • 代碼模組少

  3. CPU親和(affinity)

    • 把CPU核心和Nginx背景工作處理序綁定,把每個worker進程固定在一個cpu上執行,減少切換cpu的cache miss,獲得更好的效能。

  4. sendfile

    • 把檔案的傳輸只通過 kernel space傳輸給使用者,不經過 user space

五、Nginx的快速安裝

  1. 進入官網 http://nginx.org/

  2. 點擊 download

  3. 點擊 Linux packages for stable version

  4. 修改/etc/yum.repos.d/nginx.repo,並添加官網指定內容

注意:baseurl需要修改OS和OSRELEASE為你對應的伺服器版本
  1. 直接 yum install nginx

  2. nginx -v 出現nginx的版本資訊說明安裝成功!

六、Nginx的目錄和配置文法

  • rpm -ql nginx:可以查詢nginx安裝的檔案

  • 目錄

    • /etc/logrotate.d/nginx:設定檔,Nginx日誌輪轉,用於logrotate服務的日誌切割

    • /etc/nginx、/etc/nginx/nginx.conf、/etc/nginx/conf.d、/etc/nginx/conf.d/default.conf:目錄、設定檔,Nginx主設定檔

    • /etc/nginx/fastcgi_params、/etc/nginx/uwsgi_params、/etc/nginx/scgi_params:設定檔,cgi配置相關,fastcgi配置

    • /etc/nginx/koi-utf、/etc/nginx/koi-win、/etc/nginx/win-utf:設定檔,編碼轉換映射轉化檔案

    • /etc/nginx/mime.types:設定檔,設定http協議的Content-Type與副檔名對應關係

    • /usr/lib/systemd/system/nginx-debug.service、/usr/lib/systemd/system/nginx.service、/etc/sysconfig/negix、/etc/sysconfig/negix-debug:設定檔,配置守護進程管理器的管理方式

    • /usr/lib64/nginx/modules、/etc/nginx/modules:目錄,Nginx模組目錄

    • /usr/sbin/nginx、/usr/sbin/nginx-debug:命令,Nginx服務的啟動管理的終端命令

    • /var/cache/nginx:目錄,Nginx的緩衝目錄

    • /var/log/nginx:目錄,Nginx的日誌目錄

  • nginx -V:

  • 編譯參數

    • --with-http_stub_status_module

    • --with-http_sub_module

    • --with-http_random_index_module

    • --with-ld-opt=parameters

    • --with-cc-opt=parameters

    • --user=nginx

    • --group=nginx

    • --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

    • --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

    • 安裝目的目錄或路徑

    • 執行對應模組時,nginx所保留的臨時性檔案

    • 設定nginx進程啟動的使用者和使用者組

    • 設定額外的參數將被添加到CFLAGS變數

    • 設定附加的參數,連結系統庫

    • 目錄中隨機播放一個首頁

    • HTTP內容替換

    • Nginx的用戶端狀態

  • Nginx預設配置文法

    • worker_connections:每個進程允許最大串連數

    • use:背景工作處理序數

    • user:設定nginx服務的系統使用使用者

    • worker_processes:背景工作處理序數(最好跟cpu的數量保持一致)

    • error_log:nginx的錯誤記錄檔

    • pid:nginx服務啟動的pid

    • events:

七、Nginx日誌類型

  1. 包括了:error.log和access.log

  2. 通過nginx.conf設定檔中log_format來定義要記錄的變數格式來記錄日誌

  3. 可以被記錄到日誌中的變數

    • Nginx內建

    • arg_PARAMETER:request請求的參數

    • http_HEADER:request請求的header

    • sent_http_HEADER:服務端返回的header

    • HTTP請求變數

    • 內建變數

    • 自訂變數

八、Nginx模組

nginx -tc /etc/nginx/nginx.conf:查詢設定檔文法是否正確
nginx -s reload -c /etc/nginx/conf:重啟
  1. http_stub_status_module(展示Nginx相關資訊)

    • 配置文法:stub_status

    • 預設:無

    • Context:server,location

  2. random_index_module

    • 配置文法:random_index on|off

    • 預設:random_index off

    • Context:location

  3. http_sub_module

    • default:sub_filter_once on

    • on:只匹配第一個,off:全域匹配

    • default:sub_filter_last_modified off

    • default:無

    • string:需要被替換的字串

    • replacement:替換的字串

    • sub_filter string replacement

    • sub_filter_last_modified on|off

    • sub_filter_once on|off

注意:上述的Context:http,server,location
  1. limit_conn_module(串連頻率限制)

    • 配置文法:limit_conn zone number

    • 預設:無

    • Context:http,server,location

    • 配置文法:limit_conn_zone key zone=name:size

    • 預設:無

    • Context:http

    • limit_conn_zone

    • limit_conn

  2. limit_req_module(請求頻率限制)

    • 配置文法:limit_req zone=name [brust=number] [nodelay]

    • 預設:無

    • Context:http,server,location

    • 配置文法:limit_req_zone key zone=name:size rate=rate

    • 預設:無

    • Context:http

    • limit_req_zone

    • limit_req

  3. http_access_module(基於IP的存取控制)

    • 配置文法:deny address|CIDR(網段)|unix:|all;

    • 預設:無

    • Context:http,server,location,limit_except

    • 配置文法:allow address|CIDR(網段)|unix:|all;

    • 預設:無

    • Context:http,server,location,limit_except

    • allow

    • deny

局限性:通過代理訪問會失效

  • 可以使用http_x_forwarded_for

  • 結合geo模組

  • 通過http自訂變數傳遞

  1. http_auth_basic_module(基於使用者的信任登入)

    • 配置文法:auth_basic_user_file filePath

    • 預設:無

    • Context:http,server,location,limit_except

    • 配置文法:auth_basic string | off;

    • 預設:無

    • Context:http,server,location,limit_except

    • auth_basic

    • auth_basic_user_file

注意:file的格式是指定的,產生密碼可以使用httpd-tools
命令htpasswd -c filePath username

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.