nginx反向代理+负载均衡+https

摘要:
A服务器(192.168.133.1)作为nginx代理服务器访问B服务器(192.168.133.2)作为后端真实服务器访问https://www.test.com请求从服务器A到服务器B上的服务器A 192.168.133.1的反向代理1)编译并安装nginx省略2)配置nginx cd/usr/local/nginx/confvinginx.confuserwww;工人过程

  A服务器(192.168.133.1)作为nginx代理服务器

  B服务器(192.168.133.2)作为后端真实服务器

访问https://www.test.com请求从A服务器上反向代理到B服务器上

A服务器192.168.133.1的操作流程

1)编译安装nginx省略

2)配置nginx

cd /usr/local/nginx/conf

vi nginx.conf

user  www;
worker_processes  8;
 
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
 
events {
    worker_connections  65535;
}
   
http {
    include       mime.types;
    default_type  application/octet-stream;
    charset utf-8;
  
    log_format  main  '$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_cookie" $host $request_time';
    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;
    keepalive_timeout  65;
  
  
    fastcgi_connect_timeout 3000;
    fastcgi_send_timeout 3000;
    fastcgi_read_timeout 3000;
    fastcgi_buffer_size 256k;
    fastcgi_buffers 8 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_intercept_errors on;
   
      
    client_header_timeout 600s;
    client_body_timeout 600s;
   
    client_max_body_size 100m;     
    client_body_buffer_size 256k;   <br>       
   ## support more than 15 test environments<br>    server_names_hash_max_size 512;<br>    server_names_hash_bucket_size 128;<br>
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types       text/plainapplication/x-javascripttext/cssapplication/xmltext/javascriptapplication/x-httpd-php;
    gzip_vary on;
   
  
    include vhosts/*.conf;
}
ulimit -n 655350
mkdir vhosts
 
****************************************************************************************************************
接下来手动配置ssl证书
如果自己手动颁发证书的话,那么https是不被浏览器认可的,就是https上面会有一个大红叉
推荐一个免费的网站:https://www.startssl.com/
startssl的操作教程看这个:http://www.freehao123.com/startssl-ssl/
****************************************************************************************************************
cd /usr/local/nginx/conf/
mkdir ssl &&  cd ssl/
openssl genrsa -des3 -out aoshiwei.com.key 1024
Generating RSA private key, 1024 bit long modulus
................................++++++
....................................++++++
e is 65537 (0x10001)
Enter pass phrase for aoshiwei.com.key:                    #提示输入密码,比如这里我输入123456
Verifying - Enter pass phrase for aoshiwei.com.key:     #确认密码,继续输入123456

[root@linux-node1 ssl]# ls                                       #查看,已生成CSR(Certificate Signing Request)文件
aoshiwei.com.key

[root@linux-node1 ssl]# openssl req -new -key aoshiwei.com.key -out aoshiwei.com.csr
Enter pass phrase for aoshiwei.com.key:                      #输入123456
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]:cn                                                         #国家
State or Province Name (full name) []:beijing                                               #省份
Locality Name (eg, city) [Default City]:beijing                                               #地区名字
Organization Name (eg, company) [Default Company Ltd]:huanqiu                 #公司名
Organizational Unit Name (eg, section) []:Technology                                     #部门
Common Name (eg, your name or your server's hostname) []:huanqiu            #CA主机名
Email Address []:wangshibo@xqshijie.cn                                                      #邮箱

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456                                                                   #证书请求密钥,CA读取证书的时候需要输入密码
An optional company name []:huanqiu                                                          #-公司名称,CA读取证书的时候需要输入名称

[root@linux-node1 ssl]# ls
aoshiwei.com.csr aoshiwei.com.key

[root@linux-node1 ssl]# cp aoshiwei.com.key aoshiwei.com.key.bak
[root@linux-node1 ssl]# openssl rsa -in aoshiwei.com.key.bak -out aoshiwei.com.key
Enter pass phrase for aoshiwei.com.key.bak:                            #输入123456
writing RSA key
[root@linux-node1 ssl]# openssl x509 -req -days 365 -in aoshiwei.com.csr -signkey aoshiwei.com.key -out aoshiwei.com.crt
Signature ok
subject=/C=cn/ST=beijing/L=beijing/O=huanqiu/OU=Technology/CN=huanqiu/emailAddress=wangshibo@xqshijie.cn
Getting Private key
[root@linux-node1 ssl]# ll
total 24
-rw-r--r-- 1 root root 960 Sep 12 16:01 aoshiwei.com.crt
-rw-r--r-- 1 root root 769 Sep 12 15:59 aoshiwei.com.csr
-rw-r--r-- 1 root root 887 Sep 12 16:01 aoshiwei.com.key
-rw-r--r-- 1 root root 963 Sep 12 16:01 aoshiwei.com.key.bak

**************************************************************************************************************
nginx配置反向代理
cd /usr/local/nginx/conf/vhost
cat www.test.com-ssl.conf
upstream 8090 {
    server 192.168.1.2:8090 max_fails=3 fail_timeout=30s;
}

server {
   listen 443;
   server_name testwww.huanqiu.com;
   ssl on;

   ### SSL log files ###
   access_log logs/ssl-access.log;
   error_log logs/ssl-error.log;

### SSL cert files ###
   ssl_certificate ssl/aoshiwei.com.crt;      #由于这个证书是自己手动颁发的,是不受信任的,访问时会有个“大叉”提示,但是不影响访问https://testwww.huanqiu.com
   ssl_certificate_key ssl/aoshiwei.com.key;   #如果是线上环境,可以购买被信任后的证书,拷贝过来使用。
   ssl_session_timeout 5m;

   location / {
   proxy_pass https://8090;                                      #这个一定要是https
   proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Proto https;
   proxy_redirect off;
}
}

重启nginx
[root@linux-node1 ssl]# /usr/local/nginx/sbin/nginx -t
[root@linux-node1 ssl]# /usr/local/nginx/sbin/nginx -s reload

[root@linux-node1 ssl]# lsof -i:443
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 15755 nobody 24u IPv4 4717921 0t0 TCP *:https (LISTEN)
nginx 15756 nobody 24u IPv4 4717921 0t0 TCP *:https (LISTEN)
nginx 15757 nobody 24u IPv4 4717921 0t0 TCP *:https (LISTEN)
nginx 15758 nobody 24u IPv4 4717921 0t0 TCP *:https (LISTEN)

A服务器要开启防火墙了,则需要在iptables里开通443端口的访问
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT

 /etc/init.d/iptables restart

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*******************************************************************************************************************
后端真实服务器(192.168.133.2)上的nginx配置
vim www.test.com-ssl.conf
server {
   listen 8090;                                                                    #这里后端服务器的https没有采用默认的443端口

   server_name testwww.huanqiu.com;
   root /var/www/vhosts/test.huanqiu.com/httpdocs/main/;

   ssl on;
   ssl_certificate /Data/app/nginx/certificates/xqshijie.cer;          

#这是后端服务器上的证书,这个是购买的被信任的证书,可以把它的证书拷贝给上面的代理机器使用ssl_certificate_key /Data/app/nginx/certificates/xqshijie.key;   #可以将这两个证书拷给上面192.168.1.8的/usr/loca/nginx/conf/ssl下使用,修改nginx代理配置部分的证书路径即可!

   ssl_session_timeout 5m;

   ssl_protocols SSLv2 SSLv3 TLSv1;
   ssl_ciphers HIGH:!aNULL:!MD5;
   ssl_prefer_server_ciphers on;

   access_log /var/www/vhosts/test.huanqiu.com/logs/clickstream_ssl.log main;


location / {
   try_files $uri $uri/ @router;
   index index.php;
}

   error_page 500 502 503 504 /50x.html;

location @router {
   rewrite ^.*$ /index.php last;
}

location ~ .php$ {
  fastcgi_pass 127.0.0.1:9001;
  fastcgi_read_timeout 300;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  #include fastcgi_params;
  include fastcgi.conf;
  fastcgi_param HTTPS on;        #这个一定要加上,否则访问https时会出现报错:The plain HTTP request was sent to HTTPS port
}
} ##end server

 lsof -i:8090
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 24373 root 170u IPv4 849747 0t0 TCP *:8090 (LISTEN)
nginx 25897 nobody 170u IPv4 849747 0t0 TCP *:8090 (LISTEN)
nginx 25898 nobody 170u IPv4 849747 0t0 TCP *:8090 (LISTEN)

最后在浏览器里访问https://testwww.huanqiu.com就能通过192.168.1.1服务器反向代理到192.168.1.2上的8090端口上了~

 
 
 
 
 
 
 
 
 
 
 
 

免责声明:文章转载自《nginx反向代理+负载均衡+https》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇原有vue项目接入typescriptSQL Server统计信息简介下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

如何在Linux Mint Cinnamon中安装MATE桌面

如何在Linux Mint Cinnamon中安装MATE桌面 Linux Mint有两个版本,Cinnamon版本更漂亮,消耗资源也高,Mate版本资源需求较低,如果你安装了Cinnamon版本,想切换到mate版本又不想重装系统的话,来看看这篇经验吧。 打开菜单——》系统管理——》软件管理器 在搜索框中输入 mint-meta-mate,出现如图所示搜...

elasticsearc进行全文索引高亮显示

首先使用composer安装扩展 composer require elasticsearch/elasticsearch composer require nunomaduro/collision 开启你的 elasticsearch  与 kibana HTML代码  使用vue进行数据渲染 <!doctype html> <htm...

linux使用xampp安装MediaWiki环境

1、下载并安装xampp 下载xampp 在下载页面下载。 放置到相应目录 将xampp-linux-x64-5.6.3-0-installer.run文件复制到部署机器的/root目录下 安装 [root@vm-xa66bomoy2e ~]# cd /root [root@vm-xa66bomoy2e ~]# chmod +x xampp-linux-x...

华为S5300系列交换机限制特定IP可以登录Web

针对Web管理可能有如下需求: 1、限制某个特定IP允许访问Web 2、默认修改80端口访问 那么针对上面的设置可以有效杜绝而已Web密码暴力破解,增强交换机安全等。 实现: 1、限制特定IP登录Web,实现原理是使用ACL进行控制 [Huawei] acl 2000 // 配置ACL编号为2000。 [Huawei-acl-basic-2000] r...

linux查看修改线程默认栈空间大小(ulimit -s)

1.linux查看修改线程默认栈空间大小 ulimit -s a、通过命令 ulimit -s 查看linux的默认栈空间大小,默认情况下 为10240 即10M b、通过命令 ulimit -s 设置大小值 临时改变栈空间大小:ulimit -s 102400, 即修改为100M c、可以在/etc/rc.local 内 加入 ulimit -s 102...

如何使用Linux套接字?

      我们知道许多应用程序,例如E-mail、Web和即时通信都依靠网络才能实现。这些应用程序中的每一个都依赖一种特定的网络协议,但每个协议都使用相同的常规网络传输方法。许多人都没有意识到网络协议本身存在漏洞。本文将会学习如何使用套接字使应用程序访问网络以及如何处理常见的网络漏洞。 图1  OSI模型 1.套接字 套接字是通过操作系统(O...