LNMP架構三

Nginx代理(正向代理)

正向代理:讓局域網內的用戶 訪問外網,外網不能訪問局域網,php

場景:若是要從國內訪問美國的服務器會很慢,這時候就能夠找個香港服務器作代理,香港訪問美國是很快的。html

代理服務器做爲用戶和web服務器的代理者。mysql

 1.新建代理服務器配置文件linux

[root@bogon /]# cd /usr/local/nginx/conf/vhost
[root@bogon vhost]# vim proxy.conf

配置:(由於是代理服務器,不用訪問本機的配置文件)nginx

server
{
    listen 80;
    server_name ask.apelearn.com;                定義域名

    location /
    {
        proxy_pass      http://121.201.9.155/;          告訴nginx真正的web服務器地址
        proxy_set_header Host   $host;                 (訪問的域名是server_name)
        proxy_set_header X-Real-IP      $remote_addr;           (定義公網ip)
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   (定義代理服務器ip)
    }
}
[root@bogon vhost]# /usr/local/nginx/sbin/nginx -t            
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@bogon vhost]# /usr/local/nginx/sbin/nginx -s reload     
[root@bogon vhost]# curl -x127.0.0.1:80 ask.apelearn.com/robots.txt (檢查:(從本機訪問到了遠程站點,說明代理成功))
#
# robots.txt for MiWen
#

User-agent: *

Disallow: /?/admin/
Disallow: /?/people/
Disallow: /?/question/
Disallow: /account/
Disallow: /app/
Disallow: /cache/
Disallow: /install/
Disallow: /models/
Disallow: /crond/run/
Disallow: /search/
Disallow: /static/
Disallow: /setting/
Disallow: /system/
Disallow: /tmp/
Disallow: /themes/
Disallow: /uploads/
Disallow: /url-*
Disallow: /views/
Disallow: /*/ajax/[root@bogon vhost]# 

常見502的問題

1.配置錯誤
由於nginx找不到php-fpm了,因此報錯,通常是fastcgi_pass後面的路徑配置錯誤了,後面能夠是socket或者是ip:port


2.資源耗盡
lnmp架構在處理php時,nginx直接調取後端的php-fpm服務,若是nginx的請求量偏高,咱們又沒有給php-fpm配置足夠的子進程,那麼php-fpm就會資源耗盡,一旦資源耗盡nginx找不到php-fpm就會出現502錯誤,

解決方案
去調整php-fpm.conf中的pm.max_children數值,使其增長,可是也不能無限增長,畢竟資源有限,通常4G內存機器若是跑php-fpm和nginx,不跑mysql能夠設置爲150,8G爲300以此類推


3.除了上面的兩種錯誤還有其餘的緣由,不多有,咱們能夠藉助nginx的錯誤日誌來進行排查vim /usr/local/nginx/logs/nginx_error.log  咱們也能夠給日誌定義級別vim/usr/local/nginx/conf/nginx.conf 找到error_log,默認是crit最嚴謹的就行,也能夠改爲debug顯示的信息最全面,可是很容易撐爆咱們的磁盤。
web

[root@localhost ~]# vim/usr/local/php/etc/php-fpm.conf

[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log =/usr/local/php/var/log/php-fpm.log
[www]
listen = /tmp/www.sock
user = php-fpm
group = php-fpm
listen.owner = nobody    //定義屬主
listen.group = nobody    //定義屬組
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024

配置完以後重啓php-fpm
[root@wqslinux ~]# /etc/init.d/php-fpm restart
ps: 再補充一個,是近期不少人遇到的問題
這種狀況下,使用的是socket,版本高於5.4(含5.4) 默認監聽的socket文件權限是全部者只讀,屬組和其餘用戶沒有任何權限。因此,nginx的啓動用戶(我們配置的是nobody)就沒有辦法去讀這個socket文件,最終致使502,這個問題能夠在nginx的錯誤日誌中發現。解決辦法很簡單,上面給出的配置文件中就有避免這個問題的配置。
listen.owner = nobody //定義屬主
listen.group = nobody //定義屬組
這兩個配置就是定義socket的屬主和屬組是誰。除了這個還有一種方法
listen.mode = 777
這樣nobody也能夠有讀取權限了。ajax

Nginx負載均衡 

負載均衡:單從字面上的意思來理解就能夠解釋N臺服務器平均分擔負載,不會由於某臺服務器負載高宕機而某臺服務器閒置的狀況。那麼負載均衡的前提就是要有多臺服務器才能實現,也就是兩臺以上便可。算法

 1.在開始部署負載均衡以前,咱們先來介紹一個命令,dig命令須要yum安裝一下sql

[root@bogon ~]# yum install -y bind-utils

2.dig後加域名,他能夠返回2個ip.實則域名解析,咱們就用這兩個ip測試負載均衡vim

[root@bogon ~]# dig qq.com
[root@bogon ~]# dig qq.com

; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7 <<>> qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 37483
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1280
;; QUESTION SECTION:
;qq.com.                                IN      A

;; ANSWER SECTION:
qq.com.                 33      IN      A       125.39.240.113
qq.com.                 33      IN      A       61.135.157.156

;; Query time: 20 msec
;; SERVER: 10.21.9.12#53(10.21.9.12)
;; WHEN: 一 2月 12 19:09:33 CST 2018
;; MSG SIZE  rcvd: 67

3.再來編寫一個配置文件,須要用到upstream模塊,upstream:數據轉發功能,爲nginx提供了跨越單機的橫向處理能力,使nginx擺脫只能爲終端節點提供單一功能的限制,而使它具有了網路應用級別的拆分、封裝和整合的戰略功能。

 

upstream qq                            
{
    ip_hash;           (負載均衡有多個web服務器,咱們須要一個長鏈接來保持於一個服務器的連接,這裏須要用到hash)
    server 61.135.157.156:80;       
    server 125.39.240.113:80;
}
server
{
    listen 80;
    server_name www.qq.com;
    location /
    {
        proxy_pass      http://qq;   (這裏寫的要與upstream一致,由於域名是虛擬的,下面的2個ip纔是重要的)
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

4.檢查語法錯誤而且從新加載配置文件,返回的是qq頁面的源代碼

[root@bogon ~]# vim /usr/local/nginx/conf/vhost/load.conf 
[root@bogon ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@bogon ~]# /usr/local/nginx/sbin/nginx -s reload     
[root@bogon ~]# curl -x127.0.0.1:80 www.qq.com  

5.nginx不支持代理Https服務。也就是說不支持訪問web服務器的443端口。

ssl原理

1.https和http相比,https的通訊是加密的。若是不加密,好比你訪問一個很重要的網站,數據包仍是會到達,可是可能會用人從中間複製一份。https會把數據包加密,就算從中間複製也沒法解碼。

2.https的工做流程:

1.瀏覽器發送一個https的請求給服務器。

2.服務器有一套加解密的證書private私鑰解密,public公鑰

3.服務器會把公鑰傳輸給客戶端

4.客戶端(瀏覽器)收到公鑰後,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串隨機字符串,並用收到的公鑰加密。

5.客戶端把加密的隨機字符串傳輸給服務器

6.服務器收到加密隨機字符串後,先用私鑰解密,獲取到這一串隨機數後,再用這串隨機字符串加密傳輸的數據(該加密爲對稱加密,也就是將數據和這個隨機字符串經過某種算法混合一塊兒,這一除非知道私鑰,不然沒法獲7.取數據內容)

7.服務器把加密後的數據傳輸給客戶端。

8.客戶端收到數據後,在用本身的私鑰也就是那個隨機字符串解密。

生產ssl密鑰對

1.將祕鑰對放到conf目錄下,查看是否安裝了openssl工具,沒有安裝須要安裝。

[root@bogon ~]# cd /usr/local/nginx/conf/
[root@bogon conf]# rpm -qf `which openssl`
openssl-1.0.2k-8.el7.x86_64
[root@bogon conf]# 

2.使用openssl生成名字爲tmp.key,rsa格式的祕鑰,長度爲2048

[root@bogon conf]# openssl genrsa -des3 -out tmp.key 2048
Generating RSA private key, 2048 bit long modulus
.....+++
......................................................................................+++
e is 65537 (0x10001)
Enter pass phrase for tmp.key:
Verifying - Enter pass phrase for tmp.key:
[root@bogon conf]# ls
fastcgi.conf            koi-utf             nginx.conf.bak       uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          vhost
fastcgi_params.default  mime.types.default  scgi_params.default  win-utf
htpasswd                nginx.conf          tmp.key
[root@bogon conf]# 

3.去掉私鑰密碼並命名爲linux.key,刪除tmp.key

[root@bogon conf]# openssl rsa -in tmp.key -out linux.key
Enter pass phrase for tmp.key:
writing RSA key
[root@bogon conf]# rm -f tmp.key 
[root@bogon conf]# 

4.生成證書請求文件(用這個文件和私鑰一塊兒生成一個公鑰)  

[root@bogon conf]# openssl  req -new -key linux.key -out linux.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:china
string is too long, it needs to be less than  2 bytes long
Country Name (2 letter code) [XX]:11
State or Province Name (full name) []:BeiJing
Locality Name (eg, city) [Default City]:BeiJing
Organization Name (eg, company) [Default Company Ltd]:linux
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:linux
Email Address []:linux@qq.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:root
An optional company name []:linux
[root@bogon conf]# 

5.生成公鑰

[root@bogon conf]# openssl x509 -req -days 365 -in linux.csr -signkey linux.key -out linux.crt
Signature ok
subject=/C=11/ST=BeiJing/L=BeiJing/O=linux/OU=linux/CN=linux/emailAddress=linux@qq.com
Getting Private key
[root@bogon conf]# 

Nginx配置ssl

1.添加配置文件

[root@bogon conf]# cd vhost/
[root@bogon vhost]# vim ssl.conf
[root@bogon vhost]# 

 

server
{
    listen 443;                                        (監聽443端口)
    server_name linux.com;                                 (編寫server_name)
    index index.html index.php;
    root /data/wwwroot/linux.com;
    ssl on;                                       (開啓ssl服務)
    ssl_certificate linux.crt;                           (指定公鑰)
    ssl_certificate_key linux.key;                         (指定私鑰)
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;                   (指定三種模式)
}

2.建立linux.com目錄

[root@bogon vhost]# mkdir /data/wwwroot/linux.com
[root@bogon vhost]# /usr/local/nginx/sbin/nginx -t(若是nginx編譯的時候沒有加上ssl,這裏會報錯須要從新編譯)
nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhost/ssl.conf:7
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@bogon vhost]# 

 

[root@bogon vhost]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
configure arguments: --prefix=/usr/local/nginx
[root@bogon vhost]# 

3.編譯

[root@bogon vhost]# cd /usr/local/src/nginx-1.12.2/
[root@bogon nginx-1.12.2]# ./configure --help |grep -i ssl
  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL
[root@bogon nginx-1.12.2]#  ./configure --prefix=/usr/local/nginx/ --with-http_ssl_module
make && make install

4.重啓服務

[root@bogon nginx-1.12.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx/ --with-http_ssl_module
[root@bogon nginx-1.12.2]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
[root@bogon nginx-1.12.2]# /etc/init.d/nginx restart
Restarting nginx (via systemctl):                          [  肯定  ]
[root@bogon nginx-1.12.2]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      92257/nginx: master 
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      4161/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1475/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1477/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2778/master         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      92257/nginx: master 
tcp6       0      0 :::3306                 :::*                    LISTEN      2410/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      1475/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1477/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      2778/master         
[root@bogon nginx-1.12.2]# 

5.建立測試文件

[root@bogon nginx-1.12.2]# cd /data/wwwroot/linux.com/
[root@bogon linux.com]# ls
[root@bogon linux.com]# vim index.html (寫入ssl)

6.編輯hosts文件,添加127.0.0.1 linux.com 

[root@bogon linux.com]# vim /etc/hosts
[root@bogon linux.com]# 

7.訪問報錯,由於是咱們本身辦法的證書,直接修改/etc/hosts,用Curl測試並看不出效果,提示證書已經失去信任(緣由是,這是咱們本身辦法的瀏覽器是不信任的,只用權威的)。

[root@bogon linux.com]# curl https://linux.com
curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.
[root@bogon linux.com]# 

8.編輯windown配置文件添加10.21.95.122  linux.com,訪問,訪問不通,查看是否有防火牆,關閉防火牆:簡單的辦法直接iptables -F或者添加443的規則

[root@bogon linux.com]# iptables -nvL

9.訪問linux.com成功

 10.12306網站是本身頒發的證書:(在中國的政府有些網站,認爲只有本身的頒發的安全,因此用本身頒發的證書)

若是想要買證書,能夠搜索 沃通,

相關文章
相關標籤/搜索