前后端分离项目使用nginx配置负载均衡和https证书
前端时间,由于公司需要接口访问使用https的方式,因为我们的项目是前后端分离的形式,使用nginx进行反向代理,所以就研究了一下如何使用nginx配置服务的负载均衡和https证书的配置,下面记录了详细的步骤
一、Nginx配置SSL实现Https访问
实现https一般需要向一些第三方机构去申请证书,不过大都是收费的,这里使用openssl生成免费的证书
使用 OpenSSL 生成 SSL Key 和 CSR 文件
1.下载openssl软件 http://slproweb.com/products/Win32OpenSSL.html
配置openssl的环境变量
变量名: OPENSSL_HOME 变量值:E:\openssl\OpenSSL-Win64\bin; (变量值为openssl安装位置)
在path变量结尾添加如下 : %OPENSSL_HOME%;
2.在你的nginx安装根目录下面新建ssl文件,名称可以自定义,目录也可以是其他地方,以管理员身份进入命令行模式,进入ssl文件夹。 命令为: cd E:\nginx-1.16.1\ssl
(1)创建私钥
在命令行中执行命令: openssl genrsa -des3 -out insight.key 1024
输入密码后,再次重复输入确认密码。记住此密码,后面会用到。
(2)创建csr证书
在命令行中执行命令: openssl req -new -key insight.key -out insight.csr (key文件为刚才生成的文件,insight为自定义文件名)
输入命令后会首先校验密码,就是上一个生成私钥步骤设置的密码,输入密码后出现上台的界面会要求输入以下信息
C:Country ,单位所在国家,为两位数的国家缩写,如: CN 就是中国
ST 字段: State/Province ,单位所在州或省
L 字段: Locality ,单位所在城市 / 或县区
O 字段: Organization ,此网站的单位名称;
OU 字段: Organization Unit,下属部门名称;也常常用于显示其他证书相关信息,如证书类型,证书产品名称或身份验证类型或验证内容等;
CN 字段: Common Name ,网站的域名或者你服务器的ip地址;
输入的信息中最重要的为 Common Name,这里输入的域名即为我们要使用https访问的域名或者服务的ip。
完成信息的输入后会在ssl目录下面出现下图的两个文件
(3)去除密码
在加载SSL支持的Nginx并使用上述私钥时除去必须的口令,否则会在启动nginx的时候需要输入密码。
复制insight.key并重命名为insight.key.org
可以使用此命令行,也可以使用鼠标操作 copy insight.key insight.key.org
去除口令,在命令行中执行此命令: openssl rsa -in insight.key.org -out insight.key (insight为自定义文件名)
此命令需要输入刚才设置的密码。
(4)生成crt证书
在命令行中执行此命令: openssl x509 -req -days 365 -in insight.csr -signkey insight.key -out insight.crt (insight为自 定义文件名
至此,证书生成已经完成,ssl文件夹中一共生成如下4个文件,我们需要使用到的是insight.crt和insight.key。
使用配置nginx配置文件
找到nginx安装目录下面的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 {
upstream insights{
server 192.168.2.135:8081;
server 192.168.2.135:8082;
}
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.135;
charset utf-8;
ssl_certificate E://nginx-1.16.1//ssl//insight.crt;
ssl_certificate_key E://nginx-1.16.1//ssl//insight.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://insights;
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;
}
#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
#
}
这里说明一下 upstream insights 这个是负载均衡的配置,
ssl_certificate E://nginx-1.16.1//ssl//insight.crt;
ssl_certificate_key E://nginx-1.16.1//ssl//insight.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;
这段配置是配置证书的,至此nginx的配置也完成了,重启nginx就可以了