一、服务器(192.168.1.13)关闭Selinux
不关闭会导致反向代理接口时一直502
setenforce 0
二、服务器(192.168.1.13)关闭防火墙
# 关闭
systemctl stop firewalld.service
# 禁用
systemctl disable firewalld.service
三、服务器(192.168.1.13)安装nginx
centos7安装nginx 附赠jdk8_centos7 nginx需要安装jdk吗-CSDN博客
四、部署springboot项目
此处跳过
五、尝试代理springboot项目,nginx.conf配置
注:nginx.conf 路径位置 /etc/nginx
user root;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.1.13:8888/;
}
}
}
浏览器访问 http://192.168.1.13/cs 验证通过
六、配置本地HTTPS(免费证书)
进入服务器nginx配置项路径:
cd /etc/nginx
生成秘钥key,运行:
$ openssl genrsa -des3 -out server.key 2048
会有两次要求输入密码,输入同一个即可。输入密码然后你就获得了一个server.key文件。
以后使用此文件(通过openssl提供的命令或API)可能经常回要求输入密码,如果想去除输入密码的步骤可以使用以下命令:
$ openssl rsa -in server.key -out server.key
创建服务器证书的申请文件server.csr,运行:
openssl req -new -key server.key -out server.csr
其中Country Name填CN,Common Name填主机名也可以不填,如果不填浏览器会认为不安全.(例如你以后的url为https://abcd/xxxx….这里就可以填abcd),其他的都可以不填.
创建CA证书:
openssl req -new -x509 -key server.key -out ca.crt -days 3650
此时,你可以得到一个ca.crt的证书,这个证书用来给自己的证书签名.
创建自当前日期起有效期为期十年的服务器证书server.crt:
openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey server.key -CAcreateserial -out server.crt
ls你的文件夹,可以看到一共生成了5个文件:
其中,server.crt和server.key就是你的nginx需要的证书文件.
修改nginx.conf配置
user root;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
upstream douzi {
server 192.168.1.13:8888;
}
server {
listen 80;
server_name localhost;
server_name 192.168.1.13;
server_name 127.0.0.1;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443 ssl;
server_name localhost;
server_name 192.168.1.13;
server_name 127.0.0.1;
ssl_certificate server.crt;
ssl_certificate_key server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://douzi;
proxy_set_header WD-X-FORWARD-FOR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}