nginx网站开启https访问,http自动跳转到https
2016-04-14 17:04
1006 查看
最近nginx要开启https访问,希望服务器与客户端之间传输内容是加密的,防止中间监听泄露信息,下面是颁发证书和配置过程。
安装了openssl和openssl-devel
#yum install openssl
#yum install openssl-devel
然后就是自己颁发证书给自己
#cd /usr/local/nginx/conf (自己的配置文件目录)
#openssl genrsa -des3 -out server.key 1024
#openssl req -new -key server.key -out server.csr
#openssl rsa -in server.key.org -out server.key
#openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
至此证书已经生成完毕,下面就是配置nginx
server {
listen 443;
ssl on;
ssl_certificate /usr/local/nginxtest/conf/sites/server.crt;
ssl_certificate_key /usr/local/nginxtest/conf/sites/server.key;
}
测试配置文件nginx -t
然后重启nginx即可。
问题:
ps: 如果出现“[emerg] 10464#0: unknown directive "ssl" in /usr/local/nginx-0.6.32/conf/nginx.conf:74”则说明没有将ssl模块编译进nginx,在configure的时候加上“--with-http_ssl_module”即可^^
让浏览器信任自己颁发的证书,具体方法:
控制面板 -> Internet选项 -> 内容 -> 发行者 -> 受信任的根证书颁发机构 -> 导入 -》选择server.crt
完整nginx配置文件
server {
listen 82;
server_name *.域名;
rewrite ^(.*) https://$host$1 permanent;
}
#访问http82端口的时候自动跳转到https
server {
# PATH:/usr/local/nginxtest/conf/sites/ind.conf
listen 444;
charset utf-8;
server_name *.域名;
root /usr/local/nginxtest/ana/public;
index index.html index.htm index.php;
client_max_body_size 4m;
error_log /tmp/nginx_ind_error.log;
access_log /tmp/nginx_ind_access.log;
# ssl
ssl on;
ssl_certificate /usr/local/nginxtest/conf/sites/server.crt;
ssl_certificate_key /usr/local/nginxtest/conf/sites/server.key;
# 开启gzip
gzip on;
gzip_min_length 5k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 3;
gzip_types text/plain application/json application/javascript text/css application/xml text/javascript image/jpeg image/gif image/png;
gzip_vary on;
# 压缩资源不存在
if (!-f $request_filename) {
rewrite ^/dest/(.*)$ /static/$1 break;
}
try_files $uri $uri/ @rewrite;
location @rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param HTTPS on;
}
}
参考:
http://www.cnblogs.com/yun007/p/3739182.html http://blog.csdn.net/kongqz/article/details/9250689 http://www.cnblogs.com/tintin1926/archive/2012/07/12/2587311.html
安装了openssl和openssl-devel
#yum install openssl
#yum install openssl-devel
然后就是自己颁发证书给自己
#cd /usr/local/nginx/conf (自己的配置文件目录)
#openssl genrsa -des3 -out server.key 1024
#openssl req -new -key server.key -out server.csr
#openssl rsa -in server.key.org -out server.key
#openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
至此证书已经生成完毕,下面就是配置nginx
server {
listen 443;
ssl on;
ssl_certificate /usr/local/nginxtest/conf/sites/server.crt;
ssl_certificate_key /usr/local/nginxtest/conf/sites/server.key;
}
测试配置文件nginx -t
然后重启nginx即可。
问题:
ps: 如果出现“[emerg] 10464#0: unknown directive "ssl" in /usr/local/nginx-0.6.32/conf/nginx.conf:74”则说明没有将ssl模块编译进nginx,在configure的时候加上“--with-http_ssl_module”即可^^
让浏览器信任自己颁发的证书,具体方法:
控制面板 -> Internet选项 -> 内容 -> 发行者 -> 受信任的根证书颁发机构 -> 导入 -》选择server.crt
完整nginx配置文件
server {
listen 82;
server_name *.域名;
rewrite ^(.*) https://$host$1 permanent;
}
#访问http82端口的时候自动跳转到https
server {
# PATH:/usr/local/nginxtest/conf/sites/ind.conf
listen 444;
charset utf-8;
server_name *.域名;
root /usr/local/nginxtest/ana/public;
index index.html index.htm index.php;
client_max_body_size 4m;
error_log /tmp/nginx_ind_error.log;
access_log /tmp/nginx_ind_access.log;
# ssl
ssl on;
ssl_certificate /usr/local/nginxtest/conf/sites/server.crt;
ssl_certificate_key /usr/local/nginxtest/conf/sites/server.key;
# 开启gzip
gzip on;
gzip_min_length 5k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 3;
gzip_types text/plain application/json application/javascript text/css application/xml text/javascript image/jpeg image/gif image/png;
gzip_vary on;
# 压缩资源不存在
if (!-f $request_filename) {
rewrite ^/dest/(.*)$ /static/$1 break;
}
try_files $uri $uri/ @rewrite;
location @rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param HTTPS on;
}
}
参考:
http://www.cnblogs.com/yun007/p/3739182.html http://blog.csdn.net/kongqz/article/details/9250689 http://www.cnblogs.com/tintin1926/archive/2012/07/12/2587311.html
相关文章推荐
- TCP/IP详解学习笔记(4)-ICMP协议,ping和Traceroute
- Android学习之使用HttpURLConnection上传单多个文件
- Picasso的基本用法及如何加载网络图片和本地图片
- TCP/IP详解之IP协议ARP协议和RARP协议
- TCP/IP详解学习笔记(2)-数据链路层
- Android学习之使用HttpURLConnection上传单个文件
- TCP/IP详解学习笔记(1)-基本概念
- Volley的网络请求实例
- HTTP协议
- 在https的tomcat环境中,使某些特殊的资源通过http来访问
- 网络数据包发送接收全过程
- epoll + 多线程实现并发网络连接处理
- iOS开发网络篇—发送GET和POST请求(使用NSURLSession)
- 使用virtualbox设置双网卡,桥接+内部网络
- 近期的安卓学习开发SOS APP的一些总结文档 (拨打电话 加载首页图片的异步代码 判断是否有网络代码)
- 模板方法模式
- 快速Android开发系列网络篇之Retrofit
- HTTP状态代码
- Android 主流网络请求框架 Android-async-http
- 配置本地IIS 启用Https访问