负载均衡
在http代码块中添加
upstream upstream_server {
##java服务地址
server 192.168.1.191:7001;
server 192.168.1.192:7001;
}
location / {
root html;
index index.html index.htm;
##反向代理(java服务地址)
proxy_pass http://upstream_server;
proxy_set_header Host $host;
proxy_set_header Connection close;
proxy_connect_timeout 100ms; # 代理机器连接超时时长(默认的60s太长了)
expires 30d;
}
http跳转HTTPS
##server配置begin
server {
##监听80和443端口
listen 80;
listen 443 ssl;
##服务地址域名
server_name www.aaa.com;
if ($scheme != https) {
#rewrite ^(.*)$ https://$host$1 permanent;
return 301 https://$host$request_uri;
}
##server配置end
# HTTPS 专用配置 begin
# http://nginx.org/en/docs/http/configuring_https_servers.html
ssl_certificate /usr/local/nginx/nginx.crt;
ssl_certificate_key /usr/local/nginx/nginx.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # SSL(包括 v3)都有漏洞,应该用 TLS(TLS1.0 = SSL 3.1)
ssl_ciphers HIGH:!aNULL:!MD5;
# HTTPS 专用配置 end
}
添加头信息让浏览器加载http资源(该方法只适用于静态资源)
server中添加:
add_header 'Content-Security-Policy' 'upgrade-insecure-requests';
这一句可以让浏览器加载http的资源。但是有的浏览器不支持,比如IE
或者在页面的head中加入:
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
意思是自动将http的不安全请求升级为https
完整配置
#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;
##3.1负载均衡(第3.2步中反向代理需要用到该配置)
upstream upstream_server {
##(java)服务地址,配置俩即负载均衡到两台java服务,配置一个地址即只负载到一台(无负载)
server 192.168.1.190:7001;
server 192.168.1.190:7001;
}
ignore_invalid_headers on;
underscores_in_headers on;
merge_slashes on;
#include /etc/nginx/conf.d/*.conf; ## 各个 server{} 块
##1.https跳转配置
server {
listen 80;
listen 443 ssl;
server_name www.aaa.com;
if ($scheme != https) {
#rewrite ^(.*)$ https://$host$1 permanent;
return 301 https://$host$request_uri;
}
#charset koi8-r;
##2.让浏览器自动升级请求为https
##这一句可以让浏览器加载http的资源。但是有的浏览器不支持,比如IE
add_header 'Content-Security-Policy' 'upgrade-insecure-requests';
#access_log logs/host.access.log main;
##3.2反向代理到服务地址
location / {
root html;
index index.html index.htm;
##反向代理到服务地址
proxy_pass http://upstream_server;
proxy_set_header Host $host;
proxy_set_header Connection close;
proxy_connect_timeout 100ms; # 代理机器连接超时时长(默认的60s太长了)
expires 30d;
}
#4.HTTPS专用配置(证书配置)
# http://nginx.org/en/docs/http/configuring_https_servers.html
##证书
ssl_certificate /usr/local/nginx/nginx.crt;
##证书
ssl_certificate_key /usr/local/nginx/nginx.key;
# SSL(包括 v3)都有漏洞,应该用 TLS(TLS1.0 = SSL 3.1)
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# 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 {
# listen 443 ssl;
# server_name 10.201.50.2;
#
# ssl_certificate /usr/local/nginx/nginx.crt;
# ssl_certificate_key /usr/local/nginx/nginx.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
# }
}
使用OpenSSL生成证书
sudo openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /usr/local/nginx/nginx.key -out /usr/local/nginx/nginx.crt
这样自制证书,会被提示不安全。
Nginx文件
链接:https://pan.baidu.com/s/1qXJHXA8 密码:gd0w
参考文章
http://www.xitongzhijia.net/xtjc/20150910/57367.html?1441869885
https://www.cnblogs.com/hustskyking/p/upgrade-insecure-requests.html
https://googlechrome.github.io/samples/csp-upgrade-insecure-requests/index.html
阿里: