Nginx七層反代服務器 (Blog 20)

http反代、fastcgi反代

LNAMP: Nginx + Apache(php) + MariaDB
ngx_http_proxy_module <– 代理http協議,自定義請求首部
proxy_pass http://IP:PORT;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_path
proxy_cache
proxy_cache_key
proxy_cache_methods
proxy_cache_min_uses
proxy_cache_valid
proxy_cache_use_stale
proxy_connect_timeout
proxy_read_timeout
proxy_send_timeout

ngx_http_headers_module <– 自定義響應首部
add_header X-Via $server_addr; 代理修改響應給客戶端的報文

LNMP: Nginx + fpm + MariaDB
ngx_http_fastcgi_module <– 代理fastcgi協議
fastcgi_pass IP:PORT;
fastcgi_cache_path
fastcgi_cache
fastcgi_cache_key
fastcgi_cache_methods
fastcgi_cache_valid
fastcgi_keep_conn

注意:fpm是一個進程管理器,管理能力并不如apache(php)所以較好的模型是LNAMP: Nginx + Apache(php) + MariaDB
同網段的LNAMP,,LNMP可以實現,跨網段,無能為力;

架構一、靜態在本地:動態在后端(amp/p):
server {
listen 80;
server_name www.ilinux.io;
index index.php index.html

location / {
root /data/nginx/html;
}

location ~* \.php$ {
fastcgi_pass 192.168.10.11:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/apps$fastcgi_script_name;
include fastcgi_params;

proxy_pass http://192.168.10.11:80;
}

}

架構二、靜態在后端:動態在后端(amp/p):
server {
listen 80;
server_name www.ilinux.io;

location / {
proxy_pass http://192.168.10.11:80;

}
location ~* \.php$ {
fastcgi_pass 192.168.10.11:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/apps$fastcgi_script_name;
include fastcgi_params;

proxy_pass http://192.168.10.11:80;
}

}

listen Nginx 監聽在哪個地址端口接收請求,做應用層的SNAT+DNAT=fullnat;
server_name 基于主機名訪問
proxy_pass http://HOST;
HOST: IP, IP:PORT, FQDN;
proxy_pass后的斜線差距;
(1)、在正則表達式模式中,location中不能加斜線;
(2)、proxy_pass 后不加斜線;
location / {
root
}
location /admin/ {
proxy_pass http://:upstream_server_ip1:80;
}
訪問:server1/admin/ –> 后端documentRoot下存在admin;

(3)、proxy_pass 后加斜線;
location / {
root
}
location /admin/ {
proxy_pass http://upstream_server_ip1:80/;
}
訪問:server1/admin/ –> 后端documentRoot下不存在admin;
(4)、location后是 /,沒有區別;

注意: 代理至后端時,都需要配置后端DocuementRoot
proxy_pass 由后端程序決定;
fastcgi_pass 由 fastcgi_param SCRIPT_FILENAME 決定

1、如果是proxy_pass,額外附加功能:
(1)設定首部:
自定義請求首部: 代理修改發給upstremserver請求報文, ngx_http_proxy_module
目的: 后端服務器可獲取真正的客戶端地址,便于日志分析;修改后端httpd日志格式: ${X-Real-IP}i, 表示替換請求報文的X-Real-IP首部的值;
也可抓包分析請求報文首部;

proxy_set_header X-Real-IP $remote_addr;
注意: 多級代理時,應該使用:proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

自定義響應首部: 代理修改響應給客戶端的報文, ngx_http_headers_module
add_header X-Via $server_addr;

了解: 代理服務器并非只能修改首部,也可以將傳遞來的首部直接發給服務器 或 響應給客戶端
proxy_pass_header 將headers直接傳遞給代理的Client; 是upstream_server發來的指定headers;
proxy_pass_request_headers 將headers直接傳遞給代理的Server; 是client發來的請求報文headers;
proxy_pass_request_body 將body直接傳遞給代理的Server; 是client發來的請求報文body;

(2)代理緩存: 基于http協議,一般在proxy_pass生效的范圍內定義;
緩存的作用:
1、將網絡IO+磁盤IO 轉換為磁盤IO
2、基于URL hash值存儲,查找O(1);
緩存思路:
存儲在內存中,有多大;滿了存儲在磁盤中,磁盤滿了LRU;
內存中存儲時,以什么當作鍵;
哪些方法可以存,哪些內容存多久;
哪些不可以存:GET時,set_cookie,基于cookie請求,認證… 不應該緩存;
后端掛了怎么辦;
緩存清理機制:
時間內,訪問次數;
purge: 人工按需清理;原服務器發布了新版本;遺憾的是社區版不支持,varnish上是靈活使用;

動態生成的緩存是否可以緩存?
隨時可以,就看用戶是否接受過期的內容;但對數據不能緩存,對描述信息可緩存;
例如:你的QQ被點贊多少次,你現在看到過去的100次和看到現在200次沒有什么影響;

定義緩存:
http {
proxy_cache_path /data/nginx/cache levels=1:2:1 keys_zone=pcache:10m max_size=2g;
// proxy_cache_path path [levels=levels keys_zone=name:size [inactive=time] [max_size=size]
// 磁盤路徑 路徑下的層級 鍵區域=保存hash表的內存空間名字,大小 [非活動時間=10m] 最大多大空間磁盤空間:滿了LRU
}

調用緩存:
http, server, location {
//調用緩存 Default: proxy_cache off;
proxy_cache pacache;

//把什么做為hash表的鍵;
proxy_cache_key $scheme$proxy_host$request_uri;
$request_uri:多個域名指向同一個站點時,更容易命中;不同站點時,需要定義

//哪些方法緩存?
proxy_cache_methods GET HEAD;

//10分鐘內至少訪問1次緩存; 10相當于定義緩存中Inactive=10,默認的
proxy_cache_min_uses 1;

//定義不同的內容緩存時間:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;

//后端哪些情況下,可以使用緩存響應客戶端;
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | off …;
off:表示聯系不上就一定不能響應;
}

(3)超時定義:
面向服務器:proxy指定超時;
與服務器建立連接的超時時長;(<=75s) http, server, location
proxy_connect_timeout 60s;
向服務器發送請求的超時時長;到了60s即返回 502 bad gateway.
proxy_send_timeout 60s;
從服務器讀取響應的超時時長;
proxy_read_timeout 60s;

面向客戶端:
server中keepalive定義

2、如果是fastcgi_pass,額外附加功能:
(1)代理緩存:fastcgi協議,在location中定義;
定義緩存:
http {
fastcgi_cache_path /data/nginx/fcgicache levels=2:2:2 keys_zone=fcache:10m max_size=2g;
}
調用緩存:
location ~* \.php$ {
fastcgi_pass 192.168.10.11:9000;
fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /data/apps/$fastcgi_script_name; /data/apps/ 表示fpm的DocumentRoot
include fastcgi_params; <– 向后端傳遞參數

fastcgi_cache fcache;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
fastcgi_keep_conn on;
}
刪除^M:tr -d ‘\r’ < file | tee file
(2)Nginx要求保持連接,fastcgi_keep_conn on;
(3)面向fpm服務端的連接超時:
fastcgi_connect_timeout 60s;
fastcgi_send_timeout 60s;
fastcgi_read_timeout 60s;
(4)ping status
location ~* ^/(status|ping)$ {
fastcgi_pass 192.168.10.11:9000;
fastcgi_param SCRIPT_FILENAME /data/apps$fastcgi_script_name;
include fastcgi_params;
}

LNMP:
拓撲:
fpm: 192.168.10.11 eno16777736 vmnet1
mariadb-server: 192.168.10.12 eno16777736 vmnet1
Nginx:
eno16777736: 172.16.0.6 bridge
eno33554984: 192.168.10.254 vmnet1

1)橋接模式下安裝fpm,mariadb-server
fpm: yum install php-fpm php-mysql php-mbstring php-mcrypt
mariadb-server: yum install mariadb-server
2)配置fpm和mariadb-server
~]# vim /etc/php-fpm.d/www.conf
listen = 0.0.0.0:9000 <監聽在外網地址上才能接收外部請求>
;listen.allowed_clients = 127.0.0.1 <允許哪些客戶端>,注釋表示所有;iptables有訪問控制功能;
user = nginx
group = nginx
pm.max_children = 150
pm.status_path = /status
ping.path = /ping
ping.response = pong
php_value[session.save_path] = /var/lib/php/session <保存session的目錄屬主和屬組應該與php-fpm進程的用戶相同>
~]# useradd -r nginx
~]# install -d -o nginx -g nginx /var/lib/php/session
~]# systemctl start php-fpm.service
~]# ss -tnl (*:9000)
~]# ps axu
nginx 2479 0.0 0.6 331300 5032 ? S 14:07 0:00 php-fpm: pool www

~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
log_bin=mysql-bin
~]# systemctl start mariadb.service
~]# ss -tnl (*:3306)
~]# ps axu
mysql 2520 0.0 0.2 115344 1704 ? Ss 14:10 0:00 /bin/sh /usr/bin/mysqld_safe –basedi
~]# mysql_secure_installation
~]# mysql -uroot -pmagedu
MariaDB [(none)]> GRANT ALL ON *.* TO ‘admin’@’%’ IDENTIFIED BY ‘pass’;
MariaDB [(none)]> FLUSH PRIVILEGES;

3)安裝配置Nginx。靜態在本地:動態在后端(p)
~]# yum install nginx
~]# cd /etc/nginx
虛擬主機配置:
1)打開nginx.conf
2)查看http { .. }上下文中是否存在片段化配置選項:
此處捕捉:
http {
include /etc/nginx/conf.d/*.conf;
}

配置虛擬主機:
# vim conf.d/vhost1.conf
server {
listen 80;
server_name www.ilinux.io;

location / {
root /data/nginx/html;
}

}
# mkdir -pv /data/nginx/html
# 生成測試頁
測試:# nginx -t
啟動:# systemctl start nginx.service
查看:# ss -tnlp (*:80) (((“nginx”,3469,6))
# ps axu
網頁中輸入:http://www.ilinux.io/
172.16.0.6

配置反代fastcgi協議;
# cat conf.d/vhost1.conf
server {
listen 80;
server_name www.ilinux.io;
index index.php index.html

location / {
root /data/nginx/html;
}

location ~* \.php$ {
fastcgi_pass 192.168.10.11:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/apps$fastcgi_script_name;
include fastcgi_params;
}
}
在后端主機上創建此DocumentRoot
# mkdir -pv /data/apps
生成測試頁:
測試:# nginx -t
重載:# nginx -s reload
網頁中輸入:http://www.ilinux.io/
192.168.10.11

4)動靜分離;需要將phpMyadmin在兩個DocumentRoot中布署;
在fpm布署;
# cd /data/apps
# unzip phpMyAdmin-4.0.10.20-all-languages.zip
# ln -sv phpMyAdmin-4.0.10.20-all-languages pma
# cp pma/config.sample.inc.php pma/config.inc.php
# openssl rand -hex 4
57a1a64b
# vim pma/config.inc.php
$cfg[‘blowfish_secret’] = ’57a1a64ba8b7c6d’; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
$cfg[‘Servers’][$i][‘host’] = ‘192.168.10.12’;

網頁中輸入:http://www.ilinux.io/pma
404 Not Found
因為location只定義了\.php$ 反代到后端;
http://www.ilinux.io/pma/index.php

登陸: admin
密碼: pass
在Nginx上布署:
# cd /data/nginx/html
# unzip phpMyAdmin-4.0.10.20-all-languages.zip
# ln -sv phpMyAdmin-4.0.10.20-all-languages pma
# cp pma/config.sample.inc.php pma/config.inc.php
# openssl rand -hex 4
57a1a64b
# vim pma/config.inc.php
$cfg[‘blowfish_secret’] = ’57a1a64ba8b7c6d’; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
$cfg[‘Servers’][$i][‘host’] = ‘192.168.10.12’;

網頁中輸入:http://www.ilinux.io/pma

5)將靜態資源反代至后端主機192.168.10.12;
在12上安裝httpd服務: httpd
# systemctl start httpd.service
生成測試頁:
配置Nginx:
http {
fastcgi_cache_path /data/nginx/fcgicache levels=2:2:2 keys_zone=fcache:10m max_size=2g;
}
server {
listen 80;
server_name www.ilinux.io;
index index.php index.html;

location / {
root /data/nginx/html;
proxy_pass http://192.168.10.12:80;
}

location ~* \.php$ {
fastcgi_pass 192.168.10.11:9000;
fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /data/apps/$fastcgi_script_name;
include fastcgi_params;

fastcgi_cache fcache;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
fastcgi_keep_conn on;
}
location ~* ^/(status|ping)$ {
fastcgi_pass 192.168.10.11:9000;
fastcgi_param SCRIPT_FILENAME /data/apps$fastcgi_script_name;
include fastcgi_params;
}
}
測試、重載;

配置后端httpd:
~]# vim /etc/httpd/conf/httpd.conf
LogFormat “%{X-Real-IP}i %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined
~]# httpd -t
~]# systemctl reload httpd.service

網頁中輸入:http://www.ilinux.io/pma
Not Found
~]# tail /var/log/httpd/access_log
172.16.0.179 – – [16/Dec/2017:15:03:09 +0800] “GET /pma/ HTTP/1.0” 404 202 “-” “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36”

布署wordpress至/var/www/html
# cd /var/www/html
# unzip phpMyAdmin-4.0.10.20-all-languages.zip
# ln -sv phpMyAdmin-4.0.10.20-all-languages pma
# cp pma/config.sample.inc.php pma/config.inc.php
# vim pma/config.inc.php
$cfg[‘blowfish_secret’] = ’57a1a64ba8b7c6d’; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
$cfg[‘Servers’][$i][‘host’] = ‘192.168.10.12’;

網頁中輸入:http://www.ilinux.io/pma
是索引;需要在配置文件未中添加
DirectoryIndex index.php index.html
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.10.11:9000/data/apps/$1
測試語法、重啟httpd服務;
# httpd -t
# systemctl restart httpd.service

6) 布署wordpress至192.168.10.12, 192.168.10.11;
mariadb-server上新增用戶;
~]# mysql -uroot -pmagedu
MariaDB [(none)]> CREATE DATABASE wordpress;
MariaDB [(none)]> GRANT ALL ON wordpress.* TO ‘wpuser’@’%’ IDENTIFIED BY ‘wppass’;
MariaDB [(none)]> FLUSH PRIVILEGES;
~]# mysql -uwpuser -h192.168.10.12 -pwppass
MariaDB [(none)]>

fpm: 1.11
# pwd
/data/apps
# unzip wordpress-4.9.1-zh_CN.zip
# ln -sv wordpress wp
# cp wp/wp-config-sample.php wp/wp-config.php
# vim wp/wp-config.php

/** WordPress數據庫的名稱 */
define(‘DB_NAME’, ‘wordpress’);

/** MySQL數據庫用戶名 */
define(‘DB_USER’, ‘wpuser’);

/** MySQL數據庫密碼 */
define(‘DB_PASSWORD’, ‘wppass’);

/** MySQL主機 */
define(‘DB_HOST’, ‘192.168.10.12’);

靜態服務器上:
~]# cd /var/www/html
# unzip wordpress-4.9.1-zh_CN.zip
# ln -sv wordpress wp
# cp wp/wp-config-sample.php wp/wp-config.php
# vim wp/wp-config.php

/** WordPress數據庫的名稱 */
define(‘DB_NAME’, ‘wordpress’);

/** MySQL數據庫用戶名 */
define(‘DB_USER’, ‘wpuser’);

/** MySQL數據庫密碼 */
define(‘DB_PASSWORD’, ‘wppass’);

/** MySQL主機 */
define(‘DB_HOST’, ‘192.168.10.12’);

lnamp:

將192.168.10.11修改為一個amp即可;
~]# systemctl stop php-fpm
# yum install httpd php php-mysql php-mbstring php-mcrypt mariadb-server
~]# vim /etc/httpd/conf/httpd.conf
DocumentRoot “/data/apps”
<Directory “/data/apps”>
# systemctl start httpd.service
# vim conf.d/ilinux.conf
http {
proxy_cache_path /data/nginx/cache levels=1:1:1 keys_zone=pcache:10m max_size=2g;
}
# vim conf.d/ilinux.conf
server {
listen 80;
server_name www.ilinux.io;
index index.php index.html;

proxy_cache pcache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
proxy_cache_use_stale http_502;
location / {
root /data/nginx/html;
proxy_pass http://192.168.10.12:80;
}

location ~* \.php$ {
proxy_pass http://192.168.10.11:80;
}
}

訪問: http://www.ilinux.io/pma/index.php

https提供pma
# vim ilinux.conf
server {
listen 443 ssl http2 default_server;
server_name www.ilinux.io;
index index.php index.html;
ssl_certificate “/etc/nginx/ssl/nginx.crt”;
ssl_certificate_key “/etc/nginx/ssl/nginx.key”;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

proxy_cache pcache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
proxy_cache_use_stale http_502;
location / {
root /data/nginx/html;
proxy_pass http://192.168.10.12:80;
}

location ~* \.php$ {
proxy_pass http://192.168.10.11:80;
}
}

本地完成證書簽發;
# dir=/etc/pki/CA
# (umask 077; openssl genrsa -out $dir/private 2048)
# openssl req -new -x509 -out $dir/cacert.pem -key $dir/private/cakey.pem -days 7533
# touch $dir/index.txt
# echo 01 > $dir/serial

~]# mkdir /etc/nginx/ssl
~]# cd /etc/nginx/ssl
# (umask 077; openssl genrsa -out nginx.key 2048)
# openssl req -new -key nginx.key -out nginx.csr -days 365
# openssl ca -in nginx.csr -out nginx.crt -days 365

# systemctl restart nginx.service
# curl –cacert $dir/cacert.pem https://www.ilinux.io/pma/index.php

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/90451

(0)
逆神陽逆神陽
上一篇 2017-12-20
下一篇 2017-12-20

相關推薦

  • 第四周作業

    1. 復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限 [root@localhost ~]# chmod -R g=,o=  /home/tuser1 [root@localhost home]# ll drwx——  3 root&…

    Linux干貨 2016-12-26
  • N26-第四周作業-邢巖

    馬哥門徒-N26-邢巖   精神練習需要深入認真的工作以及熱情的勁頭。當你開始練習,你是在掙扎、在反抗,你需要集中精力,然后慢慢進步。那么,我們就開始吧。   第一題,復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。   ~]# cp -r /et…

    Linux干貨 2017-02-15
  • 零距離接觸軟RAID0和RAID5以及邏輯卷LVM

    一、創建一個可用空間為1G的RAID1設備,文件系統為ext4,有一個空閑盤,開機可自動掛載至/backup目錄 1、首先手動給虛擬機添加兩塊硬盤 2、添加硬盤后,無需關機,直接讓內核掃描添加的磁盤 [root@centos6 ~]# echo '- – -' >&nbsp…

    Linux干貨 2016-09-01
  • 第十周作業

    1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情)     CentOS系統啟動流程:     1.加電自檢:由bios去檢測各硬件是否存在且是否正常運行,然后進行硬件初始化     2.選擇啟動順序,加載mbr:根據bios設置的設備啟動順序,…

    2017-07-03
  • 文件權限管理–詳解

    進程安全上下文 進程安全上下文:     進程對文件訪問權限應用模型     進程的屬主與文件的屬主是否相同,如果相同則運行屬主權限     進程的屬主與文件的屬主不相同,對比屬組權限是否相同,相同則應用屬組權限   &…

    Linux干貨 2016-08-04
  • net25-第14周作業

    系統的INPUT和OUTPUT默認策略為DROP; ~]# iptables -P INPUT DROP ~]# iptables -P OUTPUT DROP 1、限制本地主機的web服務器在周一不允許訪問;新請求的速率不能超過100個每秒;web服務器包含了admin字符串的頁面不允許訪問;web服務器僅允許響應報文離開本機; ~]#iptables -…

    Linux干貨 2017-05-15

評論列表(1條)

  • 馬哥教育
    馬哥教育 2018-01-07 17:49

    搭建一個項目試驗的時候,建議先把拓撲圖和實驗目的寫出來,這樣更有利于閱讀和了解文章~繼續加油~

欧美性久久久久