HTTP相關術語
PV : Page Visit 頁面獨立瀏覽量,查看日志生成條數可以看到PV數量。
PV全稱Page View,中文翻譯即頁面瀏覽。其具體的度量方法是從瀏覽器發出一個對網絡服務器的請求(Request),網絡服務器接到該請求后,會將請求對應的網頁(Page)發送給瀏覽器,從而產生一個PV。只要是請求發送給了瀏覽器,無論頁面是否完全打開(下載完成),都計為1個PV。
UV : 獨立設備,訪問的次數。
IP : 同一個ip訪問的次數,比如寬帶入網,只有一個公網IP,內網有n個用戶訪問同一個頁面,那么也只有1個IP
假設公司一座大廈,大廈有100人,每個人有一台電腦和一部手機,上網都是通過nat轉換出口,每個人點擊網站兩次,請問對應的pv,uv,ip分別是多少?
pv:400
uv:200
ip:1
SOA松耦合架構
把網頁功能模塊化。
nginx的優點
-
代碼少,輕量級,性能高采用epoll模型。
epoll是Linux內核為處理大批量文件描述符而作了改進的poll,是Linux下多路復用IO接口select/poll的增強版本,它能顯著提高程序在大量並發連接中只有少量活躍的情況下的系統CPU利用率。另一點原因就是獲取事件的時候,它無須遍歷整個被偵聽的描述符集,只要遍歷那些被內核IO事件異步喚醒而加入Ready隊列的描述符集合就行了。epoll除了提供select/poll那種IO事件的水平觸發(Level Triggered)外,還提供了邊緣觸發(Edge Triggered),這就使得用戶空間程序有可能緩存IO狀態,減少epoll_wait/epoll_pwait的調用,提高應用程序效率。 -
技術成熟,用戶廣泛。
百度、騰訊、新浪、等都在使用。 -
功能模塊化,便於二次開發。
要使用什么功能就調用什么模塊,使用更加的輕便化。運行占用內存小。
nginx應用場景
靜態服務
1. 瀏覽器緩存
2. 防盜鏈
3. 資源分類
4. 資源壓縮
5. 資源緩存
6. 跨域訪問
代理服務
1、協議類型
2、正向代理
3、反向代理
4、負載均衡
5、代理緩存
6、動靜分離
安全服務
1、訪問控制
2、訪問限制
3、流量限制
4、攔截攻擊
5、攔截異常請求
6、攔截sql注入
流行架構
nginx+php lnmp
nginx+java lnmt
nginx+python
靜態和動態的區別,主要是看是否調用了數據庫。
靜態服務器: nginx、apache、IIs、lighttpd、Tengine、openresty-nginx他們沒有辦法直接連接數據庫。
動態服務器: tomcat、resin、php、weblogic、jboss
安裝nginx
nginx官網下載地址http://nginx.org/en/download.html ; 在網頁最下面,應盡量使用穩定版網頁中已經標注。
選擇相應的版本然后復制紅色部分到yum倉庫中即可。
yum安裝
# 1、修改nginx的yum源。
[root@web01 ~]# vi /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
# 2、安裝
[root@web01 ~]# yum -y install nginx
# 3、啟動服務,開機自啟
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx
# 4、檢測nginx是否安裝成功
檢查端口:
[root@web01 ~]# netstat -lntup |grep 80
檢查進程:
[root@web01 ~]# ps -ef | grep [n]ginx
# 檢查nginx的版本
[root@web01 ~]# nginx -v
# 檢查安裝時的功能
[root@web01 ~]# nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --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'
# nginx的啟動、停止、重啟、重新加載配置文件
[root@web01 ~]# systemctl restart nginx 在配置文件配置錯誤的時候容易導致服務起不起來。
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl stop nginx
[root@web01 ~]# systemctl reload nginx
# 二進制包nginx的管理
/app/nginx/sbin/nginx -s stop # 停止
/app/nginx/sbin/nginx -s reload # 重新加載配置文件
/app/nginx/sbin/nginx # 啟動
nginx的相關文件
通過官方源,yum安裝的nginx,配置文件已經被優化過。
/etc/nginx/conf.d/ | 用戶配置的配置文件目錄 |
---|---|
/etc/nginx/nginx.conf | 主配置文件 |
nginx代理文件
/etc/nginx/scgi_params # ajax
/etc/nginx/uwsgi_params # python
/etc/nginx/fastcgi_params # php
和字符集編碼相關的文件
[root@web01 ~]# ll /etc/nginx/koi-utf
-rw-r--r-- 1 root root 2837 Apr 21 23:07 /etc/nginx/koi-utf
[root@web01 ~]# ll /etc/nginx/koi-win
-rw-r--r-- 1 root root 2223 Apr 21 23:07 /etc/nginx/koi-win
[root@web01 ~]# ll /etc/nginx/win-utf
-rw-r--r-- 1 root root 3610 Apr 21 23:07 /etc/nginx/win-utf
瀏覽器支持直接打開的格式
[root@web01 ~]# ll /etc/nginx/mime.types
-rw-r--r-- 1 root root 5231 Apr 21 23:07 /etc/nginx/mime.types
Content-Type:與擴展名
[root@workstation ~]# cat /etc/nginx/mime.types
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/javascript js;
application/atom+xml atom;
application/rss+xml rss;
...
nginx相關命令文件
[root@web01 ~]# /usr/sbin/nginx
[root@web01 ~]# /usr/sbin/nginx-debug
日志相關文件,logrotate是一個系統工具,可以自動的切割日志。nginx的日志每天會自動打包切割。
[root@web01 ~]# ll /etc/logrotate.d/nginx
[root@web01 ~]# ll /var/log/nginx/
total 8
-rw-r----- 1 nginx adm 412 May 14 11:04 access.log
-rw-r----- 1 nginx adm 246 May 14 11:04 error.log
nginx的配置文件
主要分為三個模塊:核心模塊、事件驅動模塊、http模塊。
[root@web01 ~]# cat /etc/nginx/nginx.conf
########## 核心模塊 ##########
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
########## 事件驅動模塊 ##########
events {
worker_connections 1024;
}
########## http模塊 ##########
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;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
nginx配置文件詳解
[root@web01 ~]# cat /etc/nginx/nginx.conf
########## 核心模塊 ##########
# nginx的啟動用戶
user nginx;
# worker的進程數量,子進程,通常使用auto。
worker_processes 1;
# 錯誤日志的存放路徑和記錄日志的級別-info、error、warn、debug
error_log /var/log/nginx/error.log warn;
# pid文件的路徑
pid /var/run/nginx.pid;
########## 事件驅動模塊 ##########
events {
# 每個worker進程,允許可連接的數量
worker_connections 1024;
}
########## http模塊 ##########
http {
# 包含指定文件的內容;瀏覽器可加載的文件:html、jpeg、text、txt。。。。
include /etc/nginx/mime.types;
# 默認要下載的類型,在/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;
#tcp_nopush on;
# 長連接的超時時間
keepalive_timeout 65;
# 是否開啟gzip壓縮,打開之后傳輸更快
#gzip on;
# 在/etc/nginx/conf.d/以conf結尾的文件
include /etc/nginx/conf.d/*.conf;
}
使用下面的方法自定義日志格式
在此配置文件中的設置是全局的
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"';
log_format zidingyi '"訪問的IP:"$remote_addr';
access_log /var/log/nginx/access.log main;
# 日志格式的名字
access_log /var/log/nginx/zidingyi.log zidingyi;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
# =====================================================================
可以調用主配置文件中的日志格式
[root@web01 ~]# vi /etc/nginx/conf.d/gong.conf
server{
listen 80;
server_name www.gong.com;
root /code;
index index.html;
access_log /var/log/nginx/zidingyi.log zidingyi;
}