nginx

 

 

靜態小文件(1M),支持高併發,同時佔用的資源不多,3w併發,10個進程,內存150Mjavascript

配置簡單,靈活,輕量php

高併發(靜態小文件),靜態幾萬的併發css

佔用資源小,2W併發,開10個線程服務,內存消耗幾百Mhtml

支持epoll模型,使得nginx能夠支持高併發java

 

nginx的應用場景:node

靜態服務(圖片,視頻服務)html,js,css,jpg,gif等nginx

動態服務,nginx+fastcgi的方式運行php,jspjson

反向代理,負載均衡,日PV2000w下vim

 

nginx虛擬主機:併發

在配置文件中,一個server標籤就是一個虛擬主機

一、基於域名的虛擬主機,經過域名區分主機==>應用:外部網站

二、基於端口的虛擬主機,經過端口來區分虛擬主機==>應用:公司內部網站,網站後臺

三、基於IP的虛擬主機,幾乎不用

 

安裝nginx

1.安裝pcre

yum install pcre pcre-devel
yum install openssl openssl-devel -y
建立nginx用戶
useradd nginx -s /sbin/nologin -M
wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar xvf nginx-1.10.3.tar.gz
cd nginx-1.10.3
2.編譯安裝nginx
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
make && make install
3.啓動nginx
 /usr/local/nginx/sbin/nginx
netstat -tunlp | grep nginx 
查看nginx安裝的信息
/usr/local/nginx/sbin/nginx -V

nginx日誌

cat /var/log/messages

cat /usr/local/nginx/logs/error.log

conf配置文件

html網站目錄

logs日誌

sbin啓動命令

user nobody nobody;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 65535;
error_log  /usr/local/nginx/logs/nginx_error.log crit;
pid        /usr/local/nginx/logs/nginx.pid;
events 
{
  use epoll;
  worker_connections 65535;
}

http 
{
  include       mime.types;
  default_type  application/octet-stream;
  charset utf-8;  
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m; 
  sendfile on;
  tcp_nopush     on;
  keepalive_timeout 10;
  server_tokens    off;
  tcp_nodelay on;
  add_header Server-ID $hostname;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 256k;
  fastcgi_buffers 2 256k;
  fastcgi_busy_buffers_size 256k;
  fastcgi_temp_file_write_size 256k;
  fastcgi_intercept_errors on;
  #add for compress
  gzip on;
  gzip_min_length   1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 6;
  gzip_types  text/plain application/x-javascript text/css  application/xml;
  gzip_vary on;
  postpone_output 1460;
  gzip_proxied        any;
  gzip_disable        "MSIE [1-6]\.";
  client_body_buffer_size  512k;
  proxy_connect_timeout    600;
  proxy_read_timeout       600;
  proxy_send_timeout       600;
  proxy_buffer_size        8k;
  proxy_buffers            4 32k;
  proxy_busy_buffers_size 64k;
  proxy_temp_file_write_size 64k;
  proxy_temp_path   /gomeo2o/tmp/proxy_temp_dir;
  proxy_cache_path  /gomeo2o/tmp/proxy_cache_dir  levels=1:2   keys_zone=cache_one:9192m inactive=1d max_size=30g;
  proxy_set_header        Host            $host;
  proxy_set_header        Proxy-Client-IP       $remote_addr;
  proxy_set_header        X-Forwarded-For $http_x_forwarded_for;

  server {
        listen       80;
        server_name  localhost;
        location / {
                 return 200 "<h1>not found</h1>";
                 expires      30d;
        }
  }

  log_format nginxjson '{"@timestamp":"$time_iso8601",'
               '"@fields": { '
               '"@version":"1",'
               '"host":"$server_addr",'
               '"remote_addr":"$remote_addr",'
               '"http_x_forwarded_for":"$http_x_forwarded_for",'
               '"request_method":"$request_method",'
               '"domain":"$host",'
               '"url.raw":"$uri",'
               '"url":"$scheme://$http_host$request_uri",'
               '"status":"$status",'
               '"server_protocol":"$server_protocol",'
               '"size":$body_bytes_sent,'
               '"responsetime":$request_time,'
               '"http_referer":"$http_referer",'
               '"upstr_addr": "$upstream_addr",'
               '"upstr_status": "$upstream_status",'
               '"ups_resp_time": "$upstream_response_time",'
               '"x_gomeplus_token":"$http_x_gomeplus_token",'
               '"x_gomeplus_access_token":"$http_x_gomeplus_access_token",'
               '"accept":"$http_accept",'
               '"agent": "$http_user_agent"}}';
  access_log   /usr/local/nginx/logs/access.log nginxjson;
  include sites-enabled/*.conf;
}

 基於域名的虛擬主機

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
}
}

基於域名的虛擬主機配置

www.etiantian.org站點目錄/html/www

bbs.etiantian.org站點目錄/html/bbs

a.配置文件

b.站點目錄

mkdir ../html/{www,bbs} -p

echo 'www.etiantian.org' > ../html/www/index.html

echo 'bbs.etiantian.org' > ../html/bbs/index.html

c.從新加載nginx

/usr/local/nginx/sbin/nginx -t 檢查語法

/usr/local/nginx/sbin/nginx -s reload 從新加載

d.配置hosts

 

利用include優化nginx配置文件

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    #nginx vhosts config
    include extra/www.conf;
    include extra/bbs.conf;
}

cd /usr/local/nginx/conf

mkdir extra

vim bbs.conf

server {
        listen  80;
        server_name bbs.etiantian.org;
        location / {
                root    html/www;
                index   index.html  index.htm;
}
}

虛擬主機別名配置

只須要在server_name後面加上主機名,用空格隔開

server {
        listen  80;
        server_name www.etiantian.org etiantian.org;
        location / {
                root    html/www;
                index   index.html  index.htm;
}
}

一個集羣,監控節點,能夠給每臺主機一個別名

 

nginx狀態

編譯安裝時加上這個參數--with-http_ssl_module

extra/status.conf

server{
        listen  80;
        server_name  status.etiantian.org;
        location / {
          stub_status on;
          access_log  off;
   }

 }

include extra/status.conf加到http中

 

Active connections: 2    server accepts handled requests 24 24 14    Reading: 0 Writing: 1 Waiting: 1

 

Active connections: 2 nginx正處理的活動鏈接數

accepts:nginx啓動到如今共處理了24個鏈接

handled:共成功建立24次握手請求丟失數=握手數-鏈接數

requests:共處理了14次請求

Reading:讀取到客戶端的Header信息數

Writing:返回給客戶端的Header信息數

Waiting:已經處理完正在等候下一次請求指令的駐留鏈接

 

nginx日誌

  • 錯誤日誌

  常見的有debug|info|notice|warn|error|crit|alert|emerg
  通常是warn|error|crit這三個級別之一,注意不要配置info等低級別,會帶來磁盤I/O消耗

  能夠放在main,http,server,location

worker_processes  1;
error_log logs/error.log error;#logs/error.log日誌位置 error日誌級別

 

  • 訪問日誌

  log_format  用來定義記錄日誌的格式,放置在http下

  access_log  用來指定日誌文件的路徑及使用何種日誌格式記錄日誌

1.
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
2.

vim /extra/www.conf,在server外加上access_log logs/access_www.log main;#logs/access_www.log日誌位置,main和上面的log_format對應

 

 

access_log off #這裏的off,表示不記錄訪問日誌

輸出的日誌格式

10.144.52.178 - - [18/Jan/2017:15:17:39 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKi
t/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36" "-"

 

在高併發場景下


訪問日誌輪詢分割

 

 

企業應用場景

相關文章
相關標籤/搜索