nginx建https站实验

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介:

wKioL1h59KnBK0ziAABmDlW8UU0464.png

简单的lnnmp传输拓扑图

Nginx1主要是对外网提供网站服务,既然是对外网的提供服务器,那么我们就要考虑问题了。外网的网络带宽有限,这里对外网的传输我们就使用压缩传输;同时我们也希望数据的安全性,这里也需要提供https的加密传输;最重要的一点是这台服务器可以向后端转发客户端的请求。

Nginx2提供动态和静态数据给前端Nginx1,这里有一个非常严重的问题,这台主机不处理动态数据只是把动态数据转发给php-mysql,假如直接让代理服务器Nginx1把请求转发到php-fpm上。

mysql提供数据库服务,php-fpm提供PHP动态数据的处理

wKioL1h59MDSGWG8AABjbDKuF_8325.png

改良后的lnmp传输拓扑图

这样改良之后,会产生一个问题,前端的代理服务器Nginx1处理压力会变大,因为正则配置的性能完全不如直接转发。但是网站的请求有限,暂且采用这种。


# 目录


网络关系

mysql和php-fpm的配置

Nginx1的配置

Nginx2的配置

总结



# 网络关系


这里我的实验环境是一台Windows主机,使用的是vmwear,使用的虚拟机是centos7

Nginx1有两个网卡,一个设置的为桥接172.16.29.2,另一个设置的为仅主机模式192.168.95.129,安装Nginx-1.10.2

Nginx2有一个仅主机模式的网卡192.168.95.132,安装Nginx-1.10.2

php-fpm仅主机模式网卡192.168.95.131,安装php-fpm,php-mysql

MySQL仅主机模式的网卡192.168.95.130,安装mariadb-server


# mysql和php-fpm的配置


### php-fpm的配置

php-fpm依赖于http或Nginx

1
2
yum  install  php-fpm php-mysql -y
vim  /etc/php-fpm .d /www .conf
1
2
3
listen = 0.0.0.0:9000  #监听在本机的所有网卡上
listen.allowed_clients = 192.168.95.132  #允许这个ip访问
pm.status_path =  /status  #开启状态页
1
2
3
4
5
mkdir  -pv  /var/www/html/phpwind  #提供phpwind文件
cd
unzip phpwind_UTF8_8.7.1.zip -d  /nginx/html/phpwind/
chown  nginx:nginx  /var/www/html/phpwind/
chown  -R nginx:nginx  /var/www/html/phpwind/



### mysql的配置

1
2
3
4
yum isntall mairadb-server -y
mysql <<eof
grant all on pwdb.* to  'pwuser' @ '192.168.95.131'  identified by  'pwpasswd' ;
eof


# Nginx1的配置

1
2
3
4
5
6
cd
wget http: //nginx .org /packages/centos/7/x86_64/RPMS/nginx-1 .10.2-1.el7.ngx.x86_64.rpm
yum  install  . /nginx-1 .10.2-1.el7.ngx.x86_64.rpm -y
cd  /etc/nginx/conf .d
mv  default.conf{,.bak}
vim www1.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
server {  #强制使用https通信
     listen 80; 
     server_name www1.oldking.org;
     rewrite ^ https: // $server_name$request_uri? permanent;
}
server {
     listen 443;
     server_name www1.oldking.org;
     ssl on; 
     ssl_certificate  /etc/nginx/ssl/nginx .crt;
     ssl_certificate_key  /etc/nginx/ssl/nginx .key;
     location ~* \.php$ {  #把php请求发送给php-fpm主机
         fastcgi_pass 192.168.95.131:9000;
         fastcgi_index   index.php;
         fastcgi_param   SCRIPT_FILENAME  /var/www/html/phpwind/upload $fastcgi_script_name;
         include         fastcgi_params;
         fastcgi_param HTTPS on; 
     }   
     location ~* ^/(status| ping )$ {  #把php状态信息发送给php-fpm主机
         include        fastcgi_params;
         fastcgi_pass 192.168.95.131:9000;
         fastcgi_param  SCRIPT_FILENAME  $fastcgi_script_name;
     }   
     location / {  #非静态和php数据,由Nginx1提供
         root  /nginx/html/phpwind/upload ;
         index index.php;
     }
     location ~* \.(jpg|png|gif|js|css)$ {  #静态数据指向Nginx2
         proxy_pass http: //192 .168.95.132;
     }
}
1
2
3
4
5
vim .. /nginx .conf  #在http字段内添加如下内容,提供压缩传输
gzip   on;
gzip_comp_level 3;
gzip_min_length 4;
gzip_types text /xml  text /css   application /javascript ;
1
2
3
4
5
mkdir  -pv  /nginx/html/phpwind  #提供phpwind文件
cd
unzip phpwind_UTF8_8.7.1.zip -d  /nginx/html/phpwind/
chown  nginx:nginx  /nginx/html/phpwind/
chown  -R nginx:nginx  /nginx/html/phpwind/


# Nginx2的配置

1
2
3
4
5
6
cd  #安装Nginx并提供配置文件
wget http: //nginx .org /packages/centos/7/x86_64/RPMS/nginx-1 .10.2-1.el7.ngx.x86_64.rpm
yum  install  . /nginx-1 .10.2-1.el7.ngx.x86_64.rpm -y
cd  /etc/nginx/conf .d
mv  default.conf{,.bak}
vim www1.conf
1
2
3
4
5
6
7
server {
     listen 80; 
     location / { 
         root  /var/www/html/phpwind/upload ;
         index index.php;
     }   
}
1
2
3
4
5
mkdir  -pv  /nginx/html/phpwind  #提供phpwind文件
cd
unzip phpwind_UTF8_8.7.1.zip -d  /nginx/html/phpwind/
chown  nginx:nginx  /nginx/html/phpwind/
chown  -R nginx:nginx  /nginx/html/phpwind/


# 总结


全站https需要业务代码的支持,提供静态数据的服务器需要使用~*匹配然后指定,使用/来匹配的话数据会直接从Nginx2主机获取数据,并且不加处理的返回给Nginx1服务器,然后返回客户端。






      本文转自Ailu0li 51CTO博客,原文链接:http://blog.51cto.com/oldking/1892002,如需转载请自行联系原作者



相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4天前
|
安全 网络协议 应用服务中间件
一文读懂HTTPS⭐揭秘加密传输背后的原理与Nginx配置攻略
一文读懂HTTPS⭐揭秘加密传输背后的原理与Nginx配置攻略
|
4天前
|
前端开发 应用服务中间件 网络安全
nginx配置SSL证书配置https访问网站 超详细(附加配置源码+图文配置教程)
nginx配置SSL证书配置https访问网站 超详细(附加配置源码+图文配置教程)
82 0
|
4天前
|
Ubuntu 应用服务中间件 Linux
nginx 配置代理ip访问https的域名配置
nginx 配置代理ip访问https的域名配置
|
4天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
64 0
|
4天前
|
应用服务中间件 nginx
nginx配置https和直接访问静态文件的方式
nginx配置https和直接访问静态文件的方式
36 3
|
4天前
|
前端开发 应用服务中间件 网络安全
http转为https,ssl证书安装及nginx配置
http转为https,ssl证书安装及nginx配置
63 1
|
4天前
|
网络协议 Unix 应用服务中间件
如何进行 Nginx HTTPS服务器搭建
【2月更文挑战第6天】
80 0
|
4天前
|
应用服务中间件 Linux 网络安全
Linux【脚本 06】HTTPS转发HTTP安装OpenSSL、Nginx(with-http_ssl_module)及自签名的X.509数字证书生成(一键部署生成脚本分享)
Linux【脚本 06】HTTPS转发HTTP安装OpenSSL、Nginx(with-http_ssl_module)及自签名的X.509数字证书生成(一键部署生成脚本分享)
72 1
|
4天前
|
应用服务中间件 网络安全 nginx
百度搜索:蓝易云【Nginx【https配置教程】】
现在,你的 Nginx 已经配置为使用 HTTPS。访问 `https://your_domain.com` 应该能够通过 SSL 连接访问你的网站。
48 2
|
4天前
|
负载均衡 安全 应用服务中间件
Nginx + Tomcat+HTTP/HTTPS实现负载均衡实例
Nginx + Tomcat+HTTP/HTTPS实现负载均衡实例
182 0