nginx 反向代理http和https配置

简介:

nginx可以反向代理http,同样也可以代理https,只是需要ssl证书。这里推荐一个好用的证书:

https://github.com/Neilpang/acme.sh/wiki/%E8%AF%B4%E6%98%8E

步骤非常详细。

安装nginx 参照:

http://mrdeng.blog.51cto.com/3736360/1735313

编译的时候需要制定ssl模块:
 --with-http_ssl_module ,启用nginx对ssl的支持。

安装完成之后,配置反向代理的nginx的conf文件:

user  www www;


worker_processes 2;

#worker_cpu_affinity 0001 0010 0100 1000;


error_log  /opt/web/nginx_error.log  crit;


pid        /usr/local/nginx/logs/nginx.pid;


#Specifies the value for maximum file descriptors that can be opened by this process.

worker_rlimit_nofile 51200;


events

{

use epoll;

worker_connections 51200;

#multi_accept on;

}


http

{


include       mime.types; 

 default_type  application/octet-stream; 

 charset  utf-8;  

 server_names_hash_bucket_size 128;  

 client_header_buffer_size 32k; 

 large_client_header_buffers 4 32k; 

# client_max_body_size 8m; 

 sendfile on; 

 tcp_nopush     on; 

 keepalive_timeout 120;  

 fastcgi_connect_timeout 400;  

 fastcgi_send_timeout 400;  

 fastcgi_read_timeout 400; 

 fastcgi_buffer_size 64k;  

 fastcgi_buffers 4 64k;  

 fastcgi_busy_buffers_size 128k; 

 fastcgi_temp_file_write_size 128k; 

 tcp_nodelay on;  

 gzip on; 

 gzip_min_length  1k; 

 gzip_buffers     4 16k; 

 gzip_http_version 1.0; 

 gzip_comp_level 2;

 gzip_types       text/plain application/x-javascript text/css application/xml; 

 gzip_vary on; 

 server_tokens off;

client_max_body_size 512m;   #允许客户端请求的最大单个文件字节数

client_body_buffer_size 128k;  #缓冲区代理缓冲用户端请求的最大字节数

proxy_connect_timeout  600;   #跟后端服务器连接超时时间,发起握手等候响应超时时间

proxy_read_timeout   600;   #连接成功后,等候后端服务器响应时间,在后端排队中等候

proxy_send_timeout 600; #后端服务器数据回传时间,就是在规定时间内后端服务器必须传完有数

proxy_buffer_size 16k; #代理请求缓存区,这个缓存区间会保存用户的信息以供nginx进行则处理,一般只要能保存下头

信息即可

proxy_buffers 4 32k; #同上,告诉nginx保存单个用的几个Buffer最大用多大空间

proxy_busy_buffers_size 64k; #如果系统很忙可以申请用的几个更大的proxy_buffer

proxy_temp_file_write_size 64k; #缓存临时文件大小

#log format

log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '

             '$status $body_bytes_sent "$http_referer" '

             '"$http_user_agent" $http_x_forwarded_for';




upstream gw2 {

                server 172.16.88.21:80 ;

        }

server {

listen 443 ;

ssl on;

ssl_certificate /opt/ssl/xxx.com.cer;

ssl_certificate_key  /opt/ssl/xxx.com.key;

        server_name  www.xxx.com;

        location / {

                   proxy_pass http://gw2;

   proxy_redirect off;

                   limit_req zone=gw6lapp burst=100 nodelay;

                   proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;

                   proxy_set_header Host $host;

   proxy_set_header X-Forwarded-Proto https;

                   proxy_set_header X-Forwarded-For $remote_addr;

                   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

   proxy_headers_hash_max_size 51200;

     proxy_headers_hash_bucket_size 6400;


                   }

        }


server {

listen 80;

server_name www.xxx.com;


rewrite ^(.*) https://$server_name$1 permanent;


       }

配置文件就是这样,你可以访问 http://www.xxx.com 会自动跳转到https://www.xxx.com

这里使用的是nginx的rewrite功能 permanent 是实现永久跳转。

注意:

我在弄的时候 没有开443端口,整的最后搞了几个小时,偶然发现,端口没有开。注意细节,防火墙一定开端口。


笔记随笔,请大家多多指正。

本文转自  674591788  51CTO博客,原文链接:http://blog.51cto.com/mrdeng/1943644

相关文章
|
3天前
|
安全 Go
解决https页面加载http资源报错
请注意,混合内容可能导致安全性问题,因此在使用上述方法时要小心。最好的方式是尽量减少或完全消除混合内容,以确保页面的安全性。
5 0
|
8天前
|
网络协议 网络安全 数据安全/隐私保护
http和https的区别!
http和https的区别!
|
11天前
|
网络协议 安全 API
Android网络和数据交互: 什么是HTTP和HTTPS?在Android中如何进行网络请求?
HTTP和HTTPS是网络数据传输协议,HTTP基于TCP/IP,简单快速,HTTPS则是加密的HTTP,确保数据安全。在Android中,过去常用HttpURLConnection和HttpClient,但HttpClient自Android 6.0起被移除。现在推荐使用支持TLS、流式上传下载、超时配置等特性的HttpsURLConnection进行网络请求。
10 0
|
14天前
|
缓存 安全 网络协议
【面试必备】HTTP和HTTPS是什么?有什么差异?
HTTP(超文本传输协议)和HTTPS(超文本传输安全协议)是用于在互联网上传输数据的协议。它们都是应用层协议,建立在TCP/IP协议栈之上,用于客户端(如浏览器)和服务器之间的通信。
21 2
|
18天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
25 0
|
29天前
|
前端开发 应用服务中间件 网络安全
http转为https,ssl证书安装及nginx配置
http转为https,ssl证书安装及nginx配置
49 1
|
30天前
|
安全 数据安全/隐私保护
深入解析:HTTP和HTTPS的三次握手与四次挥手
在这些握手和挥手过程中,双方交换信息,协商参数,建立或关闭连接,以保证数据的可靠传输。HTTPS在此基础上加入了数字证书验证和加密通信,增加了安全性。这些步骤确保了HTTP和HTTPS协议的通信过程的稳定和安全。
116 0
|
Web App开发 应用服务中间件 nginx
Nginx 配置指令的执行顺序(学习笔记二十)
大多数 Nginx 新手都会频繁遇到这样一个困惑,那就是当同一个location配置块使用了多个 Nginx 模块的配置指令时,这些指令的执行顺序很可能会跟它们的书写顺序大相径庭。
1740 0
|
应用服务中间件 nginx
Nginx 配置指令的执行顺序(二)
我们前面已经知道,当 set 指令用在 location 配置块中时,都是在当前请求的 rewrite 阶段运行的。事实上,在此上下文中,ngx_rewrite 模块中的几乎全部指令,都运行在 rewrite 阶段,包括 Nginx 变量漫谈(二) 中介绍过的 rewrite 指令。
1258 0
|
缓存 监控 应用服务中间件
九爷带你了解 nginx 日志配置指令详解
nginx日志配置指令详解 日志对于统计排错来说非常有利的。 本文总结了nginx日志相关的配置如 access_log、log_format、open_log_file_cache、log_not_found、log_subrequest、rewrite_log、error_log。
1339 0