安装Nginx
在CentOS 7上安装Nginx可以通过yum或者源码安装,我是yum安装,以下是yum安装命令:
sudo yum install epel-release
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
配置Nginx反向代理
编辑Nginx配置文件,通常位于/etc/nginx/nginx.conf或/etc/nginx/conf.d/default.conf。添加以下内容来配置反向代理:
server {
listen 80;
server_name your_domain.com;
location /{
# root /usr/local/nginx/www;
proxy_pass http://localhost:8070;
# root html;
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;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
}
配置HTTPS
需要先去申请https证书,我是去阿里云申请的
# HTTPS server https配置
server {
listen 443 ssl;
server_name your_domain.com;
# https证书
ssl_certificate cert.pem;
# https证书
ssl_certificate_key cert.key;
ssl_session_cache shared:SSL:1m;
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 /{
# root /usr/local/nginx/www;
proxy_pass http://localhost:8070;
# root html;
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;
client_max_body_size 100m;
}
}
验证测试
启动Spring Boot项目
确保Spring Boot项目已经打包为可执行的JAR文件,并通过以下命令启动:
java -jar your-spring-boot-app.jar
配置防火墙
允许HTTP流量通过防火墙:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload
测试配置
重启Nginx服务以使配置生效:
sudo systemctl restart nginx
访问http://your_domain.com、https://your_domain.com,确认请求被正确代理到Spring Boot应用。
本文介绍了如何配置Nginx作为代理服务器,分别处理HTTP和HTTPS请求。对于HTTP,设置监听80端口,将请求代理到localhost的8070端口。HTTPS部分配置了443端口,启用了SSL,使用了cert.pem和cert.key作为证书文件,确保了安全连接。同时,配置中包含了错误页面处理和客户端最大上传文件大小的设定。
395





