Nginx常見問題:
1.相同server_name多個虛擬主機優先級訪問
server{
listen 80;
server_name testserver1 phantom.wgw.io;
location{
...
}
}php
server{
listen 80;
server_name testserver2 phantom.wgw.io;
location{
...
}
}html
實驗:
conf.d目錄下新建:
test_server1.conf ,test_server2.conf
#二者的區別是:
>> diff test_server1.conf test_server2.conf
server_name testserver1 phantom.wgw.io;
server_name testserver2 phantom.wgw.io;node
root /opt/app/code1;
root /opt/app/code2;
>> nginx -tc /etc/nginx/nginx.conf
>> nginx -s reload -c /etc/nginx.conf
結論:
相同server_name多個虛擬主機,nginx會最早讀取他先讀到的那一個
2.location匹配優先級
= 進行普通字符精確匹配,也就是徹底匹配
^~ 表示普通字符匹配,使用前綴匹配
~ \~* 表示執行一個正則匹配()python
實驗:
server{
listen 80;
server_name testserver1 phantom.wgw.io;
root /opt/app;mysql
# 精確匹配
location = /code1/ {
rewrite ^(.*)$ /code1/index.html break;
}
# 正則匹配
location ~ /code.* {
rewrite ^(.*)$ /code3/index.html break;
}
# 前綴匹配
location ^~ /code {
rewrite ^(.*)$ /code2/index.html break;
}
}linux
結論:
精確匹配 > 前綴匹配 > 正則匹配
3.Nginx的try_files的使用:
做用:按順序檢查文件是否存在
location / {
try_files $uri $uri/ /index.php;
}
說明:
首先會判斷請求的url的內容是否存在,若是存在返回,
若是沒有則在請求後加"/",相似於重定向,而後取尋找這個路徑下面是否有
,若是沒有,交給index.php處理
實驗:
server{
listen 80;
server_name testserver1 phantom.wgw.io;nginx
location / {
root /opt/app/code/cache;
try_files $uri @jave_page;
}git
location @jave_page{
proxy_pass http://127.0.0.1:9090;
}
}github
4.Nginx的alias和root的區別:sql
location /request_path/image/ {
root /local_path/image/;
}
說明:
當請求http://www....com/request_path/image/cat.png時,
它實際上請求的是:/local_path/image/request_path/image/cat.png
location /request_path/image/ {
alias /local_path/image/;
}
說明:
當請求http://www....com/request_path/image/cat.png時,
它實際上請求的是:/local_path/image/cat.png
5.用什麼方法傳遞用戶真實的IP地址:
IP1(用戶)<---->IP2(代理1)[set x_real_ip=$remote_addr]<--->IP3 IPn(代理n)<---->IP5(後端服務)[$x_real_ip=IP1]
6.其餘:
Nginx: 413 Request Entity Too Large
1.用戶上傳文件限制 client_max_body_size
502 bad gateway
2.後端服務無響應
504 Gateway Time-out
3.後端服務執行超時
Nginx性能優化:
1.性能優化考慮點
1.1瞭解當前系統結構瓶頸
1.1.1 觀察指標、壓力測試
1.2瞭解業務模型
1.2.1 接口業務類型、系統層次化結構
1.3性能與安全
2.ab接口壓力測試工具:
安裝:
yum install httpd-tools
使用
ab -n 2000 -c 2 http://127.0.0.1
說明:
-n 表示總的請求數
-c 併發數
-k 是否開啓長鏈接
實驗:
server{
listen 80;
server_name testserver1 phantom.wgw.io;
location / {
root /opt/app/code/cache;
try_files $uri @python_page;
}
location @python_page{
proxy_pass http://127.0.0.1:9090;
}
}
>> ab -n 2000 -c 2 http://127.0.0.1/wgw.html
輸出以下:
...
Concurrency Level: 2
Time taken for tests: 11.014 seconds # 總耗時
Complete requests: 2000
Failed requests: 0
Write errors: 0
Non-2xx responses: 2000 # 非200請求
Total transferred: 764000 bytes
HTML transferred: 464000 bytes
Requests per second: 181.59 [#/sec] (mean) # qps每秒多少請求
Time per request: 11.014 [ms] (mean) # 單個請求花費的時間
Time per request: 5.507 [ms] (mean, across all concurrent requests) # 服務端處理單個請求的時間
Transfer rate: 67.74 [Kbytes/sec] received # 傳輸速率,判斷網絡是否存在瓶頸
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 1
Processing: 4 11 3.3 10 34
Waiting: 4 11 3.3 10 34
Total: 4 11 3.3 10 34
...
3.系統與Nginx的性能優化
網絡:
流量,丟包等
系統:
硬件【磁盤的損壞,速率,系統負載飽和,內存使用率,系統穩定性】
服務:
鏈接的優化,內核性能的優化,對於http服務請求的優化
程序:
接口性能,處理速度,執行效率
數據庫,底層服務
3.1 文件句柄:
文件句柄:
linux/unix ---一切皆文件,文件句柄就是一個索引
設置方式:
系統全局性修改,用戶局部性修改,進程局部性修改
>> vi /etc/security/limits.conf
如加入如下兩句:
root (用戶) soft nofile 65535 # 局部
root hard nofile 65535
*(全部用戶) soft nofile 65535 # 全局
* hard nofile 65535
>> vi /etc/nginx/nginx.conf
worker_rlimit_nofile 35535 # 針對nginx進程的文件句柄的限制
3.2 cpu親和:
把進程一般不會在處理器之間頻繁遷移進程遷移的頻率小,減小性能損耗
實驗:
# 查看系統有多少個cpu
>> cat /proc/cpuinfo | grep "physical id" |sort|uniq(去重)|wc -l(統計)
# 查看cpu核心
>> cat /proc/cpuinfo | grep "cpu cores" | uniq
# 輸入top,按鍵盤1鍵就能夠展現cpu的核心數量,以及他們的進程使用率是怎樣的
>> vi /etc/nginx/nginx.conf
work_processes 16; # 啓動的進程數須要和當前主機cpu的核心數一致
worker_cpu_affinity 0000000000000001 # (總共16位對應cpu核心數,這個表明第一個進程在第一個核心上面運行) 00000000000000010 ....以此類推;
worker_cpu_affinity auto; # nginx會自動幫咱們固定好cpu不須要寫一大串了
>> ps -eo pid,args(進程名),psr(使用的cpu是哪個) | grep [n]ginx
10638 nginx: master process nginx 0
22428 nginx: worker process 0
22429 nginx: worker process 1
3.3 Nginx的通用配置優化:
user nginx;
worker_processes 2; # 啓動的進程數須要和當前主機cpu的核心數一致
worker_cpu_affinity auto; # nginx會自動幫咱們固定好cpu不須要寫一大串了
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 35535 # 針對nginx進程的文件句柄的限制,建議調整到10000以上
events {
use epoll;
worker_connections 10240; # 限制每個work的進程可以處理多少個鏈接
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
charset utf-8; # 字符集
log_format main '$http_user_agent' '$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; # 若是是靜態資源服務器的話能夠把它打開
#tcp_nodeny on; # 在動態接口,keepalive打開的狀況下能夠把他打開
keepalive_timeout 65;
gzip on;
gzip_disable "MISE [1-6]\."; # 兼容,對於IE6及如下的不進行壓縮
gzip_http_verson 1.1;
include /etc/nginx/conf.d/*.conf;
}
Nginx安全篇:
1.常見的惡意行爲:
1.1 爬蟲行爲和惡意抓取、資源盜用
1.1.1 基礎防盜鏈功能 --目的不讓惡意用戶能輕易的爬取網站對外數據
1.1.2 secure_link_module --對數據安全性提升加密驗證和失效性,適合如核心重要數據
1.1.3 access_module --對後臺、部分用戶服務的數據提供IP防控
2.常見的攻擊手段:
2.1 後臺密碼撞庫:
經過猜想密碼字典不斷對後臺系統登錄性嘗試,獲取後臺登錄密碼
方法一:後臺密碼複雜度
方法二:access_module -對後臺提供IP防控
方法三:預警機制
2.2 文件上傳漏洞:
利用這些能夠上傳的接口將惡意代碼植入到服務器中,再經過url去訪問以執行代碼
如:http://www.wgw.com/upload/1.jpg/1.php
此時,他會將1.jpg做爲php代碼執行
location ^~ /upload {
root /opt/app/images;
if ($request_filename ~* (.*)\.php){
set $php_url $1;
}
}
2.3 SQL注入:
利用未過濾/未審覈用戶輸入的攻擊方法,讓應用運行本不該該運行的SQL代碼
場景:
用戶---http-->Nginx+LUA--Fastcgi-->PHP---sql-->mariadb
環境準備:
#一、安裝環境
yum install mariadb-server mariadb
yum install php php-fpm php-mysql
systemctl start mariadb
#二、創建表格、插入測試數據
create database info;
use info;
create table users(id int(11),username varchar(64), password varchar(64), email varchar(64));
insert into users (id,username,password,email) values(1,'jeson',md5('123'),'jeson@imoocc.com');
3.配置phpserve.conf
>> vi /etc/nginx/conf.d/phpserver.conf
server {
listen 80;
server_name localhost;
root /opt/app/code;
#charset koi8-r;
access_log /var/log/nginx/log/host.access.log main;
location / {
index index.php index.htm;
}
error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /opt/app/code/$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;
#}
}
>> php-fpm -D #啓動php
>> nginx -s reload -c /etc/nginx/nginx.conf
訪問:http://192.168.205.10/login.html
帳戶: ' or 1=1#
密碼: 隨意
居然登陸成功
sql:
select * from users where username=" or 1=1#' and password='asffdafsd'
# 這裏會判斷or 1=1?返回true,後面有個#號,因此後面的代碼都被註釋不能執行了
如何防止:
1. Nginx+LUA防火牆:
===>waf[Nginx LUA]{攔截cookie類型攻擊;攔截異常post請求;攔截cc攻擊;
攔截url攻擊; 攔截arg}===>後臺服務[JAVA PHP PYTHON]
http://github.com/loveshell/ngx_lua_waf
Nginx漏洞和新版本特性:
查看版本更新描述
基於 Nginx的中間件架構:
1.瞭解需求
1.1 定義Nginx在服務體系中的角色
靜態資源服務,代理服務, 動靜分離
1.2 靜態資源服務的功能設計
類型分類,瀏覽器緩存,防盜鏈,流量限制,防資源盜用,壓縮
1.3 代理服務
協議類型,正向代理,反向代理,負載均衡,代理緩存,頭信息處理,LNMP,Proxypass,分片請求
2.設計評估
硬件: 動態資源服務:CPU,內存要求比較高
靜態資源服務:硬盤的容量轉速要求,讀寫效率比較高
系統: 用戶權限,日誌目錄存放
關聯服務:LVS,keeplive,syslog,Fastcgi
3.配置注意事項 合理配置 瞭解原理 關注日誌