centos7 ngnix环境下http转https
centos7 ngnix环境下http转https
最近在对接小程序接口的时候,对方要求必须调用https,小程序接口规定是https,那么怎么来进行配置呢?首先如果你没有搭建ngnix,那么你可以参考我以前的博文https://mp.****.net/postedit/78408001,按照步骤一步一步来即可完成安装。我用的ngnix版本是nginx-1.8.0.tar.gz, ngnix安装在/usr/local/nginx/conf此目录下
一、准备ssl证书长这个样子,截个图给大家看一下
将其放到/usr/local/nginx/conf目录下:
二、打开ngnix的配置文件nginx.conf,目录是/usr/local/nginx/conf,都在同一个目录,首先看总的配置目录
#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;
server {
listen 80;
server_name localhost;
rewrite ^(.*)$ https; # 把所有的http自动转为https
#charset koi8-r;
#access_log logs/host.access.log main;
location / { # 配置静态http代理
root /login;
index index.html index.htm;
}
#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;
server_name www.spirits.com; # 填写绑定证书的域名
ssl on;
ssl_certificate /usr/local/nginx/conf/server.crt; # 证书路径
ssl_certificate_key /usr/local/nginx/conf/server_nopwd.key; # 证书key
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
ssl_prefer_server_ciphers on;
location / { # 以下蓝色做ngnix代理
# root /#/home; #站点目录
# index index.html index.htm;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $proxy_add_x_forwarded_for;
proxy_pass http://www.spirits.com:8090; #真正的服务地址
}
}
}
启动ngnix,访问地址是:https://www.spirits.com
注意配置中的彩色部分配置即可,ngnix的启动、停止、重启在安装ngnix文章中有介绍,这里就不叙述了。