Nginx反向 Proxyhttps服務

來源:互聯網
上載者:User

標籤:nginx反向 Proxyhttps

背景:

    最近因工作需要,需在web前端做一個代理,來解決部分使用者不能訪問的需求;之前通過nginx反向 Proxy已實現對web的代理,但後來發現還有網站為https的,所以又找了些資料,整理了一下,測試完成。

方法:

    Nginx代理web網站ttxsgoto.com的相關部署和配置主要如下指令碼實現:

#!/bin/bash

path_soft=$(pwd)


function base(){

yum -y install  make gcc gcc-c++ autoconf  

}


function install(){

groupadd www

useradd -g www www


wget http://1.1.1.1/nginx/pcre-8.36.tar.gz

tar zxvf pcre-8.36.tar.gz 

cd pcre-8.36

./configure 

make && make install


wget http://1.1.1.1/nginx/nginx-1.6.2.tar.gz

tar zxvf nginx-1.6.2.tar.gz 

cd nginx-1.6.2

./configure --user=www --group=www --prefix=/usr/local/web/nginx --with-http_stub_status_module  --with-http_ssl_module

make &&make install

}

function config(){

sed -i "s#\#gzip\ \ on;#\#gzip\ \ on;\n\n include\ \ vhosts/*.conf; #g" /usr/local/web/nginx/conf/nginx.conf

mkdir /usr/local/web/nginx/conf/vhosts


cat << EOF >> /usr/local/web/nginx/conf/vhosts/ttxsgoto.com.conf

server

     {

            listen             80;

            server_name    ttxsgoto.com;

            charset GB2312;

            index index.html index.htm;

            root    /date/wwwroot/ttxsgoto.com/;

                location ~ ^/NginxStatus/ {

                        stub_status on;

                        access_log off;

                 }

         location / {

             proxy_redirect off ;

             proxy_set_header Host \$host;

             proxy_set_header X-Real-IP \$remote_addr;

             proxy_set_header REMOTE-HOST \$remote_addr;

             proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;

             client_max_body_size 50m;

             client_body_buffer_size 256k;

             proxy_connect_timeout 30;

             proxy_send_timeout 30;

             proxy_read_timeout 60;

             proxy_buffer_size 256k;

             proxy_buffers 4 256k;

             proxy_busy_buffers_size 256k;

             proxy_temp_file_write_size 256k;

             proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

             proxy_max_temp_file_size 128m;

             proxy_pass    http://ttxsgoto.com;

            }

}

server

     {

            listen             8081;

            server_name    ttxsgoto.com:8081;

            charset GB2312;

            index index.html index.htm;

            root    /date/wwwroot/ttxsgoto.com/;

                location ~ ^/NginxStatus/ {

                        stub_status on;

                        access_log off;

                 }

         location / {

             proxy_redirect off ;

             proxy_set_header Host \$host;

             proxy_set_header X-Real-IP \$remote_addr;

             proxy_set_header REMOTE-HOST \$remote_addr;

             proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;

             client_max_body_size 50m;

             client_body_buffer_size 256k;

             proxy_connect_timeout 30;

             proxy_send_timeout 30;

             proxy_read_timeout 60;

             proxy_buffer_size 256k;

             proxy_buffers 4 256k;

             proxy_busy_buffers_size 256k;

             proxy_temp_file_write_size 256k;

             proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

             proxy_max_temp_file_size 128m;

             proxy_pass    http://ttxsgoto.com:8081;

            }

}

EOF

cat << EOF >> /etc/hosts

2.2.2.2 ttxsgoto.com

EOF

ln -s /usr/local/lib/libpcre.so.1 /lib64/

ulimit -SHn 51200

}

function start(){

/usr/local/web/nginx/sbin/nginx

}

function main(){

base

install

config

start

}

main

    至此,nginx代理web的安裝和配置都已完成,驗證方法:在本地修改hosts檔案:x.x.x.x  ttxsgoto.com,通過瀏覽訪問頁面成功。

代理https的實現:

1.在/usr/local/web/nginx/conf中建立目錄ssl(建立相關ssl檔案)

openssl genrsa -des3 -out ttxsgoto.com.key 1024

openssl req -new -key ttxsgoto.com.key -out ttxsgoto.com.csr 

cp ttxsgoto.com.key ttxsgoto.com.key.orgi

openssl rsa -in ttxsgoto.com.key.orgi -out ttxsgoto.com.key

openssl x509 -req -days 365 -in ttxsgoto.com.csr -signkey ttxsgoto.com.key -out ttxsgoto.com.crt

2.在nginx.conf中增加相關配置(內容如下):

include  vhosts/ttxsgoto.com.conf;

    server {

        listen       80;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

server {

        listen       443 ssl;

        server_name  ttxsgoto.com;

        ssl on;

        ssl_certificate      ssl/ttxsgoto.com.crt;

        ssl_certificate_key  ssl/ttxsgoto.com.key;

        keepalive_timeout 60;

        ssl_protocols  SSLv2 SSLv3 TLSv1;

        ssl_prefer_server_ciphers   on;

        access_log      /usr/local/web/nginx/logs/ssl-access.log;  

        error_log       /usr/local/web/nginx/logs/ssl-error.log;  

       location / {

           proxy_pass  https://ttxsgoto.com;

           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;

        }

    }

}

3.vhosts目錄下ttxsgot.com.conf的設定檔內容:

server

     {

            listen             80;

            server_name    ttxsgoto.com;

            charset GB2312;

            index index.html index.htm;

            root    /date/wwwroot/ttxsgoto.com/;

                location ~ ^/NginxStatus/ {

                        stub_status on;

                        access_log off;

                 }

         location / {

             proxy_redirect off ;

             proxy_set_header Host $host;

             proxy_set_header X-Real-IP $remote_addr;

             proxy_set_header REMOTE-HOST $remote_addr;

             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

             client_max_body_size 50m;

             client_body_buffer_size 256k;

             proxy_connect_timeout 30;

             proxy_send_timeout 30;

             proxy_read_timeout 60;

             proxy_buffer_size 256k;

             proxy_buffers 4 256k;

             proxy_busy_buffers_size 256k;

             proxy_temp_file_write_size 256k;

             proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

             proxy_max_temp_file_size 128m;

             proxy_pass    http://ttxsgoto.com;

            }

}

server

     {

            listen             8082;

            server_name    ttxsgoto.com:8082;

            charset GB2312;

            index index.html index.htm;

            root    /date/wwwroot/ttxsgoto.com/;

                location ~ ^/NginxStatus/ {

                        stub_status on;

                        access_log off;

                 }

         location / {

             proxy_redirect off ;

             proxy_set_header Host $host;

             proxy_set_header X-Real-IP $remote_addr;

             proxy_set_header REMOTE-HOST $remote_addr;

             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

             client_max_body_size 50m;

             client_body_buffer_size 256k;

             proxy_connect_timeout 30;

             proxy_send_timeout 30;

             proxy_read_timeout 60;

             proxy_buffer_size 256k;

             proxy_buffers 4 256k;

             proxy_busy_buffers_size 256k;

             proxy_temp_file_write_size 256k;

             proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

             proxy_max_temp_file_size 128m;

             proxy_pass    http://ttxsgoto:8082;

            }

}

4.添加/etc/hosts的解析 
2.2.2.2 ttxsgoto.com

5.iptables防火牆開放相關的連接埠,像這裡開放80,8082,443給外網訪問

6.驗證修改本地hosts檔案,瀏覽器中驗證訪問成功,至此nginx反向 Proxyhttps完成!

本文出自 “天天向上goto” 部落格,請務必保留此出處http://ttxsgoto.blog.51cto.com/4943095/1579882

Nginx反向 Proxyhttps服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.