簡單測試nginx反向 Proxy和負載平衡功能的操作記錄(2)-----https代理

來源:互聯網
上載者:User

標籤:

 


背景:
A伺服器(192.168.1.8)作為nginxProxy 伺服器
B伺服器(192.168.1.150)作為後端真實伺服器

現在需要訪問https://testwww.huanqiu.com請求時從A伺服器上反向 Proxy到B伺服器上

這就涉及到nginx反向 Proxyhttps請求的配置了~~~

------------------------------------------------------------------------------------
A伺服器(192.168.1.8)上的操作流程:

1)編譯安裝nginx
[[email protected] ~]# yum install -y pcre pcre-devel openssl openssl-devel gcc
[[email protected] ~]# cd /usr/loca/src
[[email protected] ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[[email protected] ~]# tar -zxvf nginx-1.8.0.tar.gz
[[email protected] ~]# cd nginx-1.8.0
#添加www使用者,其中-M參數表示不添加使用者家目錄,-s參數表示指定shell類型

[[email protected] ~]#useradd www -M -s /sbin/nologin
[[email protected] ~]##vim auto/cc/gcc
#將這句注釋掉 取消Debug編譯模式 大概在179行
#CFLAGS="$CFLAGS -g"

#我們再配置下nginx編譯參數,編譯時間一定要添加--with-http_ssl_module,以便讓nginx支援ssl功能!
[[email protected] ~]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
[[email protected] ~]#make
[[email protected] ~]#make install clean

2)配置nginx
[[email protected] ~]# cd /usr/local/nginx/conf
[[email protected] conf]# cat nginx.conf
user nobody;
worker_processes 8;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 65535;
}

http {
include mime.types;
default_type application/octet-stream;
charset utf-8;

#######
## http setting
#######
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
fastcgi_connect_timeout 6000;
fastcgi_send_timeout 6000;
fastcgi_read_timeout 6000;
fastcgi_buffer_size 256k;
fastcgi_buffers 8 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;

##
client_header_timeout 60s;
client_body_timeout 60s;
client_max_body_size 50m;
client_body_buffer_size 256k;

## support more than 15 test environments
server_names_hash_max_size 512;
server_names_hash_bucket_size 128;

##
#gzip on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
gzip_vary on;

######
## set access log format
######i
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for" $http_cookie" $host $request_time‘;

include vhosts/*.conf;

}

 

-----------------------------------------------------
接下來手動設定ssl認證
如果自己手動頒發認證的話,那麼https是不被瀏覽器認可的,就是https上面會有一個大紅叉
****************************************************
推薦一個免費的網站:https://www.startssl.com/
startssl的操作教程看這個:http://www.freehao123.com/startssl-ssl/
****************************************************

下面是手動頒發認證的操作:
[[email protected] ~]# cd /usr/local/nginx/conf/
[[email protected] conf]# mkdir ssl
[[email protected] conf]# cd ssl/
[[email protected] 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

[[email protected] ssl]# ls                                       #查看,已產生CSR(Certificate Signing Request)檔案
aoshiwei.com.key

[[email protected] 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 []:[email protected]                                                      #郵箱

Please enter the following ‘extra‘ attributes
to be sent with your certificate request
A challenge password []:123456                                                                   #認證請求密鑰,CA讀取認證的時候需要輸入密碼
An optional company name []:huanqiu                                                          #-公司名稱,CA讀取認證的時候需要輸入名稱

 

[[email protected] ssl]# ls
aoshiwei.com.csr aoshiwei.com.key

[[email protected] ssl]# cp aoshiwei.com.key aoshiwei.com.key.bak
[[email protected] ssl]# openssl rsa -in aoshiwei.com.key.bak -out aoshiwei.com.key
Enter pass phrase for aoshiwei.com.key.bak:                            #輸入123456
writing RSA key
[[email protected] 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/[email protected]
Getting Private key
[[email protected] 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的反向 Proxy:
[[email protected] vhosts]# pwd
/usr/local/nginx/conf/vhosts
[[email protected] vhosts]# cat test.xqshijie.com-ssl.conf
upstream 8090 {
server 192.168.1.150:8090; 
}

server {
listen 443 default ssl;
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
[[email protected] ssl]# /usr/local/nginx/sbin/nginx -t
[[email protected] ssl]# /usr/local/nginx/sbin/nginx -s reload

[[email protected] 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

[[email protected] ssl]# /etc/init.d/iptables restart

 

------------------------------------------------------------------------------------
後端真是伺服器(192.168.1.150)上的nginx配置

[[email protected] vhosts]# cat test.xqshijie.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

[[email protected] vhosts]# 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.8伺服器反向 Proxy到192.168.1.150上的8090連接埠上了~

簡單測試nginx反向 Proxy和負載平衡功能的操作記錄(2)-----https代理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.