nginx代理不支持https,也就是端口443;
通过配置文件设置,就可以将特定的链接向应用服务器转发。例如 Nginx 可通过以下简单的配置,即可实现代理转发:
也就是用户在CN,想访问US的web服务器,由于路途原因访问慢,那么我们可以在CN和US的中间HK搭建一个代理服务器,CN连上HK的代理
来访问US的WEB;减少一半时间;
cd /usr/local/nginx/conf/vhost/
vim proxy.conf
代码
server
{
listen 80;
server_name ask.apelearn.com;//定义代理的域名为ask.apelearn.com
location /
{
proxy_pass http://121.201.9.155/; //设置代理域名的ip,使用nslookup 解析出来ask.apelearn.com的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;
}
}
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
使用curl访问本机的ask.apelearn.com/robots.txt文件,
curl -x127.0.0.1:80ask.apelearn.com/robots.txt
返回文件成功,说明本机已经将ask.apelearn.com/ 的请求代理转发到本机上;
## 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/
当一台服务器代理多个web站点时,就必须配置负载均衡;
vim /usr/local/nginx/conf/vhost/load.conf
代码
upstream qq_com //定义一个名词,名词任意都可;
{
ip_hash; //让统一个用户在同一个ip上;
server 61.135.157.156:80; //设置服务器ip与端口;
server 125.39.240.113:80;
}
server
{
listen 80;
server_name www.qq.com;
location /
{
proxy_pass http://qq_com; //与upstream定义的名字必须一致只是前面加http://
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
使用代理服务器访问,
curl -x127.0.0.1:80 www.qq.com
成功显示qq.com的源码页面
http默认端口为80,https默认端口为443;
http传输数据为明文,https传输数据是加密的;
http是HTTP协议运行在TCP之上。所有传输的内容都是明文,客户端和服务器端都无法验证对方的身份;
https是HTTP运行在SSL/TLS之上,SSL/TLS运行在TCP之上。所有传输的内容都经过加密,加密采用对称加密,但对称加密的密钥用服务器方的证书进行了非对称加密。此外客户端可以验证服务器端的身份,如果配置了客户端验证,服务器方也可以验证客户端的身份。
1.完成TCP三次同步握手
2.客户端验证服务器的证书,通过,进入步骤3
3.DH算法协商对称加密算法的秘钥、hash算法的秘钥
4.SSL安全加密碎到协商完成;
5.网页用加密方式传输,用协商的加密算法加密,保证数据完整和不被篡改;
正常的网站https使用的ssl证书是需要购买的,我们做实验就只需要自己生成一个就行了,但是无法在网络上流通;
yum install -y openssl
设置秘钥防止目录
cd /usr/local/nginx/conf/
注意这里要求设置密码
openssl genrsa -des3 -out tmp.key 2048
注意:这里会提示要求输入老私钥文件tmp.key的密码;
openssl rsa -in tmp.key -out test.key
删除老的私钥
rm -rf tmp.key
需要设置详细信息,可以直接回车默认
openssl req -new -key test.key -out test.csr
openssl x509 -req -days 365 -in test.csr -signkey test.key -out test.crt
注意:test.crt才是公钥,test.csr只是请求文件,test.key是私钥;
vim /usr/local/nginx/conf/vhost/ssl.conf
代码
server
{
listen 443; //设置端口为443
server_name shu.com;//设置网站域名为shu.com
index index.html index.php;
root /data/wwwroot/shu.com;//设置web的站点目录
ssl on; //开启ssl功能
ssl_certificate test.crt; //指定公钥名字
ssl_certificate_key test.key; //指定私钥名字
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
}
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
查看监听端口,有443则成功
netstat -lntp
使用https://shu.com访问,成功;
在-t检测时,错误提示
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
提示:nginx不支持ssl,这是因为我们编译安装nginx时是最简单的模式编译的,没有指定ssl;
重新编译安装nginx
查看nginx之前编译的参数
/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
进入源码包
cd /usr/local/src/nginx-1.12.2/
查询ssl需要增加配置
./configure --help |grep -i ssl
结果为:--with-http_ssl_module
重新编译nginx
./configure --prefix=/usr/local/nginx --with-http_ssl_module
make && make install
重启nginx服务
/etc/init.d/nginx restart
vim /usr/local/php-fpm/etc/php-fpm.conf
增加
include = etc/php-fpm.d/*.conf
删除
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
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
mkdir /usr/local/php-fpm/etc/php-fpm.d/
cd /usr/local/php-fpm/etc/php-fpm.d/
vim www.conf
增加
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
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
vim shu32.conf
增加
[shu32]
listen = /tmp/shu32.sock
listen.mode = 666
user = php-fpm
group = php-fpm
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
vim /usr/local/nginx/conf/vhost/test.com.conf
##解析php##
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
vim /usr/local/nginx/conf/vhost/aaa.com.conf
##解析php##
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/shu32.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;
}
php-fpm与nginx都要检测生效
/usr/local/php-fpm/sbin/php-fpm -t
/etc/init.d/php-fpm reload
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
aaa与test分别使用www与shu32
ps aux |grep php-fpm
root 2001 0.0 0.4 227292 4988 ? Ss 00:16 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 2002 0.0 0.4 227232 4724 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2003 0.0 0.4 227232 4724 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2004 0.0 0.4 227232 4724 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2005 0.0 0.4 227232 4724 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2006 0.0 0.4 227232 4728 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2007 0.0 0.4 227232 4728 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2008 0.0 0.4 227232 4728 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2009 0.0 0.4 227232 4728 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2010 0.0 0.4 227232 4728 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2011 0.0 0.4 227232 4728 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2012 0.0 0.4 227232 4728 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2013 0.0 0.5 227232 5780 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2014 0.0 0.4 227232 4728 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2015 0.0 0.4 227232 4728 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2016 0.0 0.4 227232 4728 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2017 0.0 0.4 227232 4732 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2018 0.0 0.4 227232 4732 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2019 0.0 0.4 227232 4732 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2020 0.0 0.4 227232 4732 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2021 0.0 0.4 227232 4732 ? S 00:16 0:00 php-fpm: pool shu32
php-fpm 2022 0.0 0.4 227232 4728 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2023 0.0 0.4 227232 4728 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2024 0.0 0.5 227232 5508 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2025 0.0 0.5 227232 5736 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2026 0.0 0.4 227232 4732 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2027 0.0 0.4 227232 4732 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2028 0.0 0.4 227232 4732 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2029 0.0 0.4 227232 4732 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2030 0.0 0.4 227232 4732 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2031 0.0 0.4 227232 4732 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2032 0.0 0.4 227232 4732 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2033 0.0 0.4 227232 4736 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2034 0.0 0.4 227232 4736 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2035 0.0 0.4 227232 4736 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2036 0.0 0.4 227232 4736 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2037 0.0 0.4 227232 4736 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2038 0.0 0.4 227232 4736 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2039 0.0 0.4 227232 4736 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2040 0.0 0.4 227232 4736 ? S 00:16 0:00 php-fpm: pool www
php-fpm 2041 0.0 0.4 227232 4736 ? S 00:16 0:00 php-fpm: pool www
vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
增加
request_slowlog_timeout = 1slowlog = /usr/local/php-fpm/var/log/www-slow.log
/usr/local/php-fpm/sbin/php-fpm -t
/etc/init.d/php-fpm reload
让web停顿两秒时间
vim /data/wwwroot/test.com/sleep.php
sleep(2);
echo "done";
?>
curl -x127.0.0.1:80test.com/sleep.php
访问有两秒时间才结束
提示访问test.com/sleep.php慢的原因在代码第三行
cat /usr/local/php-fpm/var/log/www-slow.log
[20-Mar-2018 00:57:28] [pool www] pid 2469
script_filename = /data/wwwroot/test.com/sleep.php
[0x00007fb91fc38298] sleep() /data/wwwroot/test.com/sleep.php:3
open_basedir 的作用是限制php在指定的目录里活动。
vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
添加如下代码
php_admin_value[open_basedir]=/data/wwwroot/test.com:/tmp/
/usr/local/php-fpm/sbin/php-fpm -t
/etc/init.d/php-fpm reload
成功200
curl -x127.0.0.1:80 test.com/admin.php -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 19 Mar 2018 17:24:44 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
将test.com改为wwtest.com
vim /usr/local/php-fpm/etc/php-fpm.d/www.conf
php_admin_value[open_basedir]=/data/wwwroot/wwtest.com:/tmp/
/usr/local/php-fpm/sbin/php-fpm -t
/etc/init.d/php-fpm reload
curl -x127.0.0.1:80 test.com/admin.php -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.2
Date: Mon, 19 Mar 2018 17:18:53 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.30
vim /usr/local/php-fpm/etc/php.ini
log_errors = On //开启错误日志记录
error_log = /usr/local/php-fpm/var/log/php_errors.log //指定错误日志目录
error_reporting = E_ALL //设置错误级别
touch /usr/local/php-fpm/var/log/php_errors.log
chmod 777 /usr/local/php-fpm/var/log/php_errors.log
/usr/local/php-fpm/sbin/php-fpm -t
/etc/init.d/php-fpm reload
cat /usr/local/php-fpm/var/log/php_errors.log
cat /usr/local/php-fpm/etc/php-fpm.d/www.conf
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
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
request_slowlog_timeout = 1
slowlog = /usr/local/php-fpm/var/log/www-slow.log
php_admin_value[open_basedir]=/data/wwwroot/wtest.com:/tmp/
pm = dynamic //动态进程管理,也可以是static
pm.max_children = 50 //最大子进程数,ps aux可以查看
pm.start_servers = 20 //启动服务时会启动的进程数
pm.min_spare_servers = 5 //定义在空闲时段,子进程数的最少数量,如果达到这个数值时,php-fpm服务会自动派生新的子进程。
pm.max_spare_servers = 35 //定义在空闲时段,子进程数的最大值,如果高于这个数值就开始清理空闲的子进程。
pm.max_requests = 500 //定义一个子进程最多处理的请求数,也就是说在一个php-fpm的子进程最多可以处理这么多请求,当达到这个数值时,它会自动退出。