跳转方式有两种
1、rewrite 方式
rewrite ^(.*) https://$server_name$1 permanent;
2、return方式
return 301 https://$server_name$request_uri;
两种方式都是添加在默认监听端口中,比如80端口下方,当访问到80端口则被重定向到https中
配置如下:
worker_processes 1;
error_log logs/error.log info;
events {
worker_connections 1024;
}
http {
include mime.types;
#log_format main '$remote_addr - $remote_user [$time_local] $request '
# '"$status" $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
keepalive_timeout 1000;
proxy_read_timeout 1200;
gzip on;
gzip_min_length 1024;
gzip_buffers 4 8k;
gzip_types text/* application/javascript application/x-javascript text/css text/plain;
gzip_http_version 1.0;
gzip_comp_level 9;
gzip_proxied any;
gzip_vary on;
#output_buffers 1 32k;
#postpone_output 1460;
client_header_timeout 2048m;
client_body_timeout 2048m;
send_timeout 2048m;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
client_header_buffer_size 256k;
large_client_header_buffers 2 256k;
server_names_hash_bucket_size 256;
charset UTF-8;
client_max_body_size 2048M;
upstream local_host {
server 127.0.0.1:8080 weight=1;
}
server {
listen 80;
server_name XXXX;
#charset koi8-r;
#rewrite ^(.*) https://$server_name$1 permanent;
return 301 https://$server_name$request_uri;
#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;
#}
}
# HTTPS server
#
server {
listen 443 ssl;
server_name XXXX;
ssl_certificate XXX.pem;
ssl_certificate_key XXXXX.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
location /xx {
proxy_pass http://local_host;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $scheme;
proxy_set_header X-Forwarded-Ssl on;
}
}
}
在配置好后,正常浏览器访问可以正常跳转https,但是出现页面调用后台接口还是默认http的时候还是会报
最后通过添加header 头信息解决了,最后做个记录。
proxy_set_header X-Forwarded-Ssl on;