apache设置反向代理和nginx设置反向代理

apache配置反向代理

在设置中打开proxy模块和proxy_http模块

在ubuntu中使用sudo a2enmod proxysudo a2enmod proxy_http

在windows中使用去掉httpd.conf中的LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_http_module modules/mod_proxy_http.so前的注释

在配置文件中设置需要配置的参数

在windows中配置httpd.conf文件,在ubuntu中配置site_enabled中具体网站的参数

ProxyRequests Off
<Proxy *>
        Require all granted
</Proxy>
ProxyPass /backward http://localhost:7000

nginx设置反向代理

在nginx.conf或者site-enable中配置

    server {
        listen       8000;
    #    listen       somename:8080;
        server_name  somename  alias  another.alias;

        location / {
                proxy_pass https://www.piwheels.org;
                proxy_ssl_server_name on;
                proxy_redirect   default;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
    }        

proxy_ssl_server_name on;是为了解决一下问题:

SSL_do_handshake() failed (SSL: error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure) while SSL handshaking to upstream

出现错误的原因:

nginx反向代理的时候默认没有将 server_name 发给上游服务(被代理的服务)。如果上游服务器配置了多个证书,这会导致上游服务器无法给出正确的证书来通信,进而导致握手失败。开启proxy_ssl_server_name指令后,nginx在与上游服务进行TLS协商时,会发送server_name。

proxy_redirect default;是为了解决:

在服务器中有可能会进行403跳转,打开该设置后,nginx会自动对域名进行替换。