举一个例子nginx的https,对nginx了解还是太少,将证书和秘钥文件放在config的目录下,名字server.pem和server.key,
1.针对webservice访问无法使用https的处理,对webservice不走https(肯定有完美解决方法,不过我没有发现)
对于所有的/hnny/的7000端口请求均会使用https协议,
对于所有的/hnny/的7002端口请求会跳转到https://localhost:7000/,然后使用https协议
对于所有的/hnny/webservice/的7002端口请求跳转到http://localhost:9080,然后使用http协议(之所以不用https协议,是因为rewrite和return均会重定向,将导致webservice服务无法正常使用)
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
server {
#监听7000端口,限定为只能使用https访问
listen 7000 ssl;
server_name localhost;
ssl_certificate server.pem;
ssl_certificate_key server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
client_max_body_size; #访问时body的最大容量
location /hnny/ {
proxy_pass http://localhost:9080;
}
#携带参数,将http转换为https(一般建议用这种,携带了请求的参数),重定向
error_page 497 https://$http_host$request_uri;
#不携带参数,将http转换为https
#error_page 497 https://$http_host$uri;
}
server {
listen 7002;
server_name localhost;
#当是webservice请求,不再使用https,直接访问真正的服务器
location /hnny/webservice/ {
proxy_pass http://localhost:9080;
}
#将请求转成https
location /hnny/ {
rewrite ^(.*)$ https://localhost:7000$1;
}
}
}
注:497 - normal request was sent to HTTPS
此虚拟站点只允许https访问时,当用http访问时nginx会报出497错误码
2.在谷歌浏览器中,进行https访问时,页面中所有的http请求会自动转换为https请求。
3.应用中页面有绝对地址访问的,可能出现下面问题:
1)就是img、js、css或者其他静态资源使用绝对地址访问(真正服务器的地址:端口)就可能会找不到资源,例如nginx代理后访问的可能是内网地址,这样外网你就无法访问
2)若果浏览器没有进行http到https的安全转换,可能又会出现跨域的问题