一、需求
介绍: 我们在部署应用时,希望用到 Nginx ,并配置上 https 。我在网上看了很多文章,感觉都不是很系统。因此写下此文,以备日后使用。
二、安装 Nginx
- 获取安装包
wget http://nginx.org/download/nginx-1.17.10.tar.gz
- 解压
tar -zxvf nginx-1.17.10.tar.gz
- 配置 with-http_ssl_moudle。很关键
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
不过直接直接会报错
./configure: error: invalid option "--with-http_ssl_moudle"
需要安装依赖包
apt-get install gcc
apt-get install libpcre3 libpcre3-dev
apt-get install zlib1g zlib1g-dev
# Ubuntu14.04的仓库中没有发现openssl-dev,由下面openssl和libssl-dev替代
#apt-get install openssl openssl-dev
sudo apt-get install openssl
sudo apt-get install libssl-dev
sudo apt-get install libpcre3 libpcre3-dev
然后再运行上面的那一句
- 安装
make
make install
三、配置 https
- 将 https 的证书存放在 /usr/local/nginx/conf 目录下,我是新建了一个文件夹 cert
- 编辑配置文件 /usr/local/nginx/conf/nginx.conf 如下:
server {
listen 80;
server_name 你的域名;
return 301 https://你的域名$request_uri;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8080;
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_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}
server {
listen 443 ssl;
server_name 你的域名;
ssl_certificate cert/你的证书crt;
ssl_certificate_key cert/你的证书key;
#ssl_session_cache shared:(解析问题,去除改括号)SSL:1m;
#ssl_session_timeout 5m;
#ssl_ciphers HIGH:!aNULL:!MD5;
ssl_session_timeout 24h;
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 / {
proxy_pass http://127.0.0.1:8080;
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_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
location /pic {
proxy_pass http://127.0.0.1:8081;
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_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}
四、SpringBoot 配置使用
- 配置如下:
server:
port: 8080
tomcat:
remote-ip-header: x-forwarded-for
protocol-header: x-forwarded-proto
port-header: X-Forwarded-Port
use-forward-headers: true