Nginx相關
1. 概述
Nginx是一種Web服務器,其具有高并發、高負荷的能力,具有以下優點:
- 穩定、系統資源消耗少、占用內存較少。
- 軟件安裝包小且定制化強。
- 具有高并發能力,可處理30000-50000個請求。
Nginx作為靜態頁面的web服務器,其主要考量其性能,非常注重效率。
配置文件(最基本的配置文件):
配置文件主要分為三個區塊:全局塊、events塊和http塊。
# 全局塊
worker_processes 1;
# 事件區塊
events {worker_connections 1024;
}
# http區塊
http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server {listen 80;server_name localhost; #改為IP地址# 反向代理location / {root html; #存放目錄index index.html index.htm;}# 錯誤頁面路由error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}
}
全局塊:用于存放niginx服務器的整體配置,包括相關的進程數、進程ID、存放路徑、日志存放路徑、用戶組等信息。
events塊:用于存放niginx服務器和用戶連接的相關配置。
http塊:用于存放一些服務器訪問控制和第三方配置,包括http全局塊和server塊。
2. 反向代理
2.1 反向代理相關
客戶端訪問時,不需要進行配置就可以進行訪問。反向代理服務器會接收客戶端的請求,然后反向代理服務器去選擇訪問,將獲取的數據返回給客戶端。這樣的好處是,使用反向代理服務器可以隱藏真實服務器的IP,暴露的是代理服務器的IP。
配置文件:
#接口端
location /police/ {proxy_pass http://192.168.1.1:8852/police/;proxy_redirect default;proxy_http_version 1.1;proxy_connect_timeout 60;proxy_send_timeout 60;proxy_read_timeout 90;
}
如果遇到以/police 請求開頭的接口,訪問http://192.168.1.1:8852/police/。
若想要定義多個端口的反向代理,需要修改代理頭(location /police/)和訪問的IP地址(proxy_pass http://192.168.1.1:8852/police/;)。
2.2 正向代理
局域網中用戶要訪問Internet,通過代理服務器來訪問服務器,則需要正向代理服務器。
3. 負載均衡
將請求從發送到單個服務器上變為發送到多個服務器上,由此實現負載均衡。
#動態服務器組
upstream dynamic_zuoyu {server localhost:8080; #tomcat 7.0server localhost:8081; #tomcat 8.0server localhost:8082; #tomcat 8.5server localhost:8083; #tomcat 9.0}
實現負載均衡有4種基本方法:輪詢法、weight權重模式、ip_hash、least_conn。
方法 | 解釋 |
---|---|
輪詢法 | 默認方式 |
weight權重 | 根據權重分配 |
ip_hash | 依據ip分配 |
least_conn | 依據最少連接時間分配 |
3.1 實現方式
-
輪詢法(default):
每個請求按照時間順序逐一分配到各個服務器
#動態服務器組 upstream dynamic_zuoyu {server localhost:8080; #tomcat 7.0server localhost:8081; #tomcat 8.0server localhost:8082; #tomcat 8.5server localhost:8083; #tomcat 9.0# server 參數#fail_timeout 最大失敗時間#max_fails 設置在fail_timeout參數設置的時間內最大失敗次數,超過則認為停機#fail_time 服務器會被認為停機的時間長度,默認為10s#backup 標記該服務器為備用服務器,當主服務器停止時,請求會被發送到它這里#down 標記服務器永久停機 }
-
weight權重(加權輪詢):
指定輪詢機率,通過weight的權重來控制訪問,訪問比率和weight成正比。
#動態服務器組 upstream dynamic_zuoyu {server localhost:8080 weight=2; #tomcat 7.0server localhost:8081; #tomcat 8.0server localhost:8082 backup; #tomcat 8.5server localhost:8083 max_fails=3 fail_timeout=20s; #tomcat 9.0 }
-
ip_hash:
通過哈希算法處理請求,當用戶再次訪問某服務器時,會自動進行定位,每個請求按訪問ip的hash結果進行分配,實現每個用戶固定訪問一個后端服務器。
#動態服務器組 upstream dynamic_zuoyu {ip_hash; #保證每個訪客固定訪問一個后端服務器server localhost:8080 weight=2; #tomcat 7.0server localhost:8081; #tomcat 8.0server localhost:8082; #tomcat 8.5server localhost:8083 max_fails=3 fail_timeout=20s; #tomcat 9.0 }
-
least_conn:
把請求轉發給連接數較少的后端服務器。
#動態服務器組 upstream dynamic_zuoyu {least_conn; #把請求轉發給連接數較少的后端服務器server localhost:8080 weight=2; #tomcat 7.0server localhost:8081; #tomcat 8.0server localhost:8082 backup; #tomcat 8.5server localhost:8083 max_fails=3 fail_timeout=20s; #tomcat 9.0 }
4. 動靜分離
將動態頁面與靜態頁面由不同的服務器來解析。
配置文件:
#訪問靜態資源服務器
location /image/ {root /var/filecenter/;
}
location /static/ {root /var/filecenter/;
}
location /car/ {root /var/filecenter/;
}
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css)$ {root /Users/dalaoyang/Downloads/static;
}
#動態頁面訪問后臺服務
#接口端location /police/ {proxy_pass http://192.168.1.1:8852/police/;proxy_redirect default;proxy_http_version 1.1;proxy_connect_timeout 60;proxy_send_timeout 60;proxy_read_timeout 90;}
5. 常用命令
yum install nginx 安裝nginx
netstat -anput|grep nginx 查看nginx進程
netstat -nltp 查看服務器端口占用情況
cd /usr/local/nginx/sbin/
./nginx 啟動
./nginx -s stop 停止
./nginx -s quit 安全退出
./nginx -s reload 重新加載配置文件 如果我們修改了配置文件,就需要重新加載。
ps aux|grep nginx 查看nginx進程
6. 完整配置文件
#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include 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 logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}