nginx理論

什麼是nginx

nginx 是一個開源且高性能, 可靠的Http web服務, 代理服務, 負載均衡html

開源: 直接獲取源代碼linux

高性能: 高併發nginx

可靠: 服務穩定web

輕量: 佔用資源少apache


常見的HTTP Web服務vim

httpd 由apache基金會centos

IIS 微軟服務器版跨域

GWS google開發瀏覽器

Openrestry 基於nginx + lua緩存

Tengline 淘寶基於nginx開發


市場佔有率: apache>IIS>nginx

nginx 在互聯網行業使用比較普遍


爲何選擇nginx

Nginx很是輕量

功能模塊少(源代碼僅保留http與核心模塊代碼, 其他不夠核心代碼會做爲插件來安裝)

代碼模塊化 (易讀, 便於二次開發, 對於開發人員很是友好)

server {
	
	location /nba {
	
	}
	
	location /cba {
	
	}

}

server {
	
	location /lxx {
	
	}
	
	location /lyy {
	
	}

}

nginx 適合作微服務, 雲架構, 中間層


nginx 採用Epool網絡模型, apache 採用select模型

  • select
    1. 當用戶發起一次請求, 會存入FD(文件描述符),
    2. select模型每次調用就會進行一次遍歷掃描FD文件描述符,默認值是1024/2048,
    3. 若是修改這個值, 會影響總體性能, 使得不少鏈接超時,
    4. 數據由內核拷貝到用戶態
  • Epool
    1. Epoll 沒有最大併發鏈接的限制,上限是最大能夠打開文件的數目,這個數字通常遠大於 2048, 通常來講這個數目和系統內存關係很大
    2. Epoll 不只會告訴應用程序有I/0 事件到來,還會告訴應用程序相關的信息,這些信息是應用程序填充的,所以根據這些信息應用程序就能直接定位到事件,而沒必要遍歷整個FD 集合

https://blog.csdn.net/foxgod/article/details/93165645


nignx 應用場景

靜態服務

  • 瀏覽器緩存
  • 防資源調用(反爬)
  • 資源分類
  • 資源壓縮
  • 資源緩存
  • 跨域訪問

代理服務

代理的優勢就是能夠緩存,流程爲PC把請求發送給代理服務器, 代理服務器訪問到了服務器, 緩存下來, 再提供給PC, 當PC下次再訪問, 則更快

代理服務器能夠限制訪問頁面大小(緩存大小), 屏蔽敏感詞彙

http://www.javashuo.com/article/p-hzbmgtqa-c.html


  • 正向代理(客戶端代理)

    "代理服務器"代理了"客戶端",去和"目標服務器"進行交互, 隱藏客戶端的IP, web服務器上只能看到代理服務器的IP


  • 反向代理(服務器代理)

    外訪內,是指以代理服務器來接受internet上的鏈接請求,而後將請求轉發給內部網絡上的服務器,並將從服務器上獲得的結果返回給internet上請求鏈接的客戶端,此時代理服務器對外就表現爲一個反向代理服務器

    使用場景就是負載均衡, 當受到攻擊時候, 暴露的IP是負載均衡的IP, web服務器不會受影響


  • 代理緩存
  • 動靜分離

安全服務

  • 訪問控制

    基於身份認證的


  • WAF (防攻擊, DDOS, CC, SQL注入)
    • 流量限制
    • 攔截攻擊
    • 攔截異常請求
    • 攔截SQL注入

流行架構

  • Nginx + PHP(Fastcgi_pass)LNMP
  • Nginx + Jave(Proxy_Pass)LNMT
  • Nginx + Python (uwsgi_pass)

nginx安裝

http://nginx.org/en/linux_packages.html#RHEL-CentOS

  • 配置yum倉庫
$ yum install yum-utils
$ vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
$ yum install nginx -y

$ nginx -v
nginx version: nginx/1.16.1

nginx配置


查看nginx的文件

$ rpm -ql nginx

nginx 主配置文件

/etc/nginx
/etc/nginx/nginx.conf
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf

cgi, Fastcgi, uwsgi配置文件

/etc/nginx/uwsgi_params
/etc/nginx/scgi_params
/etc/nginx/fastcgi_params

nginx編碼轉換映射文件

/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/win-utf

http協議的Content-Type 與擴展名

/etc/nginx/mime.types

配置文件守護進程管理器

/usr/lib/systemd/system/nginx.service
-----> systemctl reload nginx

nginx日誌輪詢, 日誌切割

/etc/logrotate.d/nginx

nginx終端管理命令

/usr/sbin/nginx
-----> nginx -s reload
/usr/sbin/nginx-debug
/usr/lib64/nginx
/usr/lib64/nginx/modules

nginx默認站點目錄

/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html

nginx的幫助手冊

/usr/share/doc/nginx-1.16.1
/usr/share/doc/nginx-1.16.1/COPYRIGHT
/usr/share/man/man8/nginx.8.gz

nginx的緩存目錄

/var/cache/nginx

nginx的日誌記錄

/var/log/nginx

查看nginx安裝的選項

$ nginx -V 2>&1 |grep 'prefix'|cut -d: -f2

--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 --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

編譯選項

  • 程序安裝目錄和路徑
--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

  • 設定nginx進程啓動用戶和組(安全)
--user=nginx 
--group=nginx

  • 設置額外的參數將被添加到CFLAGS變量
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'

  • 設置附加的參數, 連接系統庫
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

場景

  • 若是以前的nignx經過源碼安裝, 這個時候部署新的nginx服務器, 如何部署

    經過 nginx -v 得到版本

    經過nginx -V 得到configure選項


nginx配置文件

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}

coreModule

核心模塊, 全局配置, 能夠有events, http

user  nginx;   # nginx進程所使用的用戶
worker_processes  1;   # nginx運行的work進程數量(建議和CPU數量一直或auto)

error_log  /var/log/nginx/error.log warn;   # nginx錯誤日誌存放路徑
pid        /var/run/nginx.pid;   # nginx服務運行後產生的pid進程號

EventModule

事件驅動模塊

events {
    worker_connections  1024;   # 每一個worker進程支持的最大鏈接數
    use epool;    # 事件驅動模型, epoll默認
}

HttpCoreModule Http

內核模塊

http模塊曾容許有多個server曾, server主要用於配置多個網站

server層容許多個Location, Location主要用於定義網站訪問路徑

http {
    include       /etc/nginx/mime.types;   # html渲染所支持的樣式
    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  /var/log/nginx/access.log  main;
	
	
    sendfile        on;		# 是否能發送文件
    #tcp_nopush     on;

    keepalive_timeout  65;   # 長鏈接超時時間65s

    #gzip  on;	# 是否壓縮

    include /etc/nginx/conf.d/*.conf;	# 模塊導入路徑
    
    # 使用server模塊來配置網站, 每一個server{} 表明一個網站(簡稱虛擬主機)
    server {
    	listen 80;	#監聽端口
    	server_name: localhost;	# 提供服務器的域名或主機名
    	access_log host.access.log;   # 訪問日誌
    	
    	# 控制網站訪問路徑
    	location / {
    		root	/usr/share/nginx/html;   # 存放網站代碼路徑
    		index	index.html index.htm;	# 服務器返回的默認頁面文件
    	}
    	
    	# 指定錯誤代碼, 統必定義錯誤頁面, 錯誤代碼重定向到新的Location
    	error_page	500 502 503 504	/50x.html;
    }
    
    }

nginx配置網站

當咱們訪問game.weixinyu.com的時候, 訪問/weixinyu_code/裏面的頁面代碼, 而且響應頭要有business和author這兩個字段

  • 配置server模塊
$ vim /etc/nginx/conf.d/weixinyu.conf 
server {
    listen       80;
    server_name  game.weixinyu.com;


    location / {
        root   /weixinyu_code;
        index  index.html index.htm;
        add_header business ops;
        add_header authors weixinyu;
    }

}

  • 檢查nginx配置文件語法
$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

  • 建立目錄
$ mkdir /weixinyu_code

  • 關閉default.conf
$ mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.off

  • 關閉防火牆和selinux

  • 放入代碼
echo 'weixinyu' > /weixinyu_code/index.html

  • 本地hosts文件作本地DNS解析 C:\Windows\System32\drivers\etc\hosts

    須要修改文件權限, 右擊屬性-安全-Users, 所有容許

192.168.221.20  game.weixinyu.com

  • 重啓nginx, 從新載入nginx
$ systemctl restart nginx
$ nginx -s reload

  • 查看80端口
$ lsof -i:80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   9921  root    6u  IPv4  41141      0t0  TCP *:http (LISTEN)
nginx   9923 nginx    6u  IPv4  41141      0t0  TCP *:http (LISTEN)
相關文章
相關標籤/搜索