前端时间公司需要配置https访问,所以研究了一下如何使用nginx 配置https证书,下面是整个的部署过程
1.首先使用openssl生成相关的证书文件,这里就不再说明,百度上面有很详细的教程,经过这个步骤你会得到两个需要使用的文件
我把这两个文件放在nginx的安装目录下面的ssl目录下面,这个你可以 自己指定,下面是核心的nginx配置文件的配置内容
#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 {
upstream insights{
server 192.168.2.28:7020;
server 192.168.2.28:8999;
}
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;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 4;
gzip_types text/plain application/json application/x-javascript application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
server {
listen 8888 ssl;
server_name 192.168.2.28;
charset utf-8;
ssl_certificate D://XJ-Insight3.1//nginx//ssl//lee.crt;
ssl_certificate_key D://XJ-Insight3.1//nginx//ssl//lee.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
error_page 497 301 https://$http_host$request_uri;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root D:/XJ-Insight3.1/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location @req {
rewrite ^.*$ /index.html last;
}
location /upload {
rewrite ^.+upload/?(.*)$ /$1 break;
root d:/XJ-Insight3.1/upload;
try_files $uri $uri/ /index.html;
}
location /api {
rewrite ^.+api/?(.*)$ /$1 break;
include uwsgi_params;
proxy_pass http://localhost:7020;
proxy_set_header Cookie $http_cookie;
proxy_set_header Host $host:$server_port;
proxy_set_header Remote_Addr $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 1000m;
}
location /files {
root C:/file;
try_files $uri $uri/ /index.html;
}
location /webserver {
rewrite ^.+webserver/?(.*)$ /$1 break;
include uwsgi_params;
proxy_pass http://localhost:8086;
proxy_set_header Cookie $http_cookie;
proxy_set_header Host $host;
proxy_set_header Remote_Addr $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 500m;
}
#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
#
}
我这里做了一个负载均衡的处理, ssl_certificate D://XJ-Insight3.1//nginx//ssl//lee.crt; ssl_certificate_key D://XJ-Insight3.1//nginx//ssl//lee.key;这两个配置就是用来配置你生成的证书,目录是你自己存放证书的目录,这里关键一点是如果你的服务是非80端口的话,一定要加上proxy_set_header Host $host:$server_port;不然的话用https访问的时候会有ERROR_PROTOC_SSL