nginx配置https+域名訪問(wèn)springboot接口怎么操作?
目標(biāo)
通過(guò)nginx配置,實(shí)現(xiàn)通過(guò)https+域名訪問(wèn)springboot部署的tomcat接口
實(shí)現(xiàn)步驟
1、首先申請(qǐng)證書(shū)
下載適用于ngxin版本的證書(shū)(.PEM格式)

2、下載壓縮包
解壓后文件夾中存在以下幾個(gè)文件:

3、將解壓后的文件
上傳至服務(wù)器中 /etc/ssl/certs 目錄下

4、修改nginx.conf文件
(1)監(jiān)聽(tīng)自己要訪問(wèn)的端口(18380),并修改服務(wù)名為域名信息,每一個(gè)虛擬主機(jī)監(jiān)聽(tīng)不同的SSL端口(默認(rèn)端口為443)
server {
listen 18380;
listen 1443 ssl;
server_name www.xxx.com;
ssl on;
ssl_certificate /etc/ssl/certs/www.xxx.com.pem;
ssl_certificate_key /etc/ssl/certs/www.xxx.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_ciphers on;
#charset koi8-r;
#access_log logs/host.access.log main;
#前端資源地址
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
#后端接口訪問(wèn)地址
location /prod-api/ {
proxy_pass http://ip:端口/;
client_max_body_size 50m;
proxy_redirect off;
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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_cookie_path / /;
}
#靜態(tài)資源訪問(wèn)地址
location /prod-api/ryK1haERqT.txt {
alias /usr/share/nginx/html/ryK1haERqT.txt;
allow all;
autoindex on;
}(2)附完整.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 {
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;
server {
listen 18380;
listen 1443 ssl;
server_name www.xxx.com;
ssl on;
ssl_certificate /etc/ssl/certs/www.xxx.com.pem;
ssl_certificate_key /etc/ssl/certs/www.xxx.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_ciphers on;
#charset koi8-r;
#access_log logs/host.access.log main;
#前端資源地址
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
#后端接口訪問(wèn)地址
location /prod-api/ {
proxy_pass http://ip:端口/;
client_max_body_size 50m;
proxy_redirect off;
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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_cookie_path / /;
}
location /prod-api/ryK1haERqT.txt {
alias /usr/share/nginx/html/ryK1haERqT.txt;
allow all;
autoindex on;
}
#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
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}5、然后重啟nginx即可
(進(jìn)入 /usr/local/nginx/sbin,運(yùn)行./nginx 或./nginx -s reload進(jìn)行重啟)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- K8s部署Nginx集群,通過(guò)Ingress實(shí)現(xiàn)HTTPS域名訪問(wèn)過(guò)程
- Nginx?CertBot配置HTTPS泛域名證書(shū)Debian及常見(jiàn)問(wèn)題
- docker nginx + https 子域名配置詳細(xì)教程
- Nginx域名轉(zhuǎn)發(fā)https訪問(wèn)的實(shí)現(xiàn)
- 阿里云Nginx配置https實(shí)現(xiàn)域名訪問(wèn)項(xiàng)目(圖文教程)
- Nginx配置同一個(gè)域名同時(shí)支持http與https兩種方式訪問(wèn)實(shí)現(xiàn)
相關(guān)文章
基于Nginx實(shí)現(xiàn)一個(gè)灰度上線系統(tǒng)的示例代碼
本文主要介紹了基于Nginx實(shí)現(xiàn)一個(gè)灰度上線系統(tǒng)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Nginx 中實(shí)現(xiàn)請(qǐng)求的排隊(duì)機(jī)制的過(guò)程詳解
Nginx中的請(qǐng)求排隊(duì)機(jī)制就像是交通警察指揮交通一樣,讓網(wǎng)絡(luò)流量有序地流動(dòng),保障服務(wù)器的穩(wěn)定運(yùn)行和用戶的良好體驗(yàn),在深入探討 Nginx 中的請(qǐng)求排隊(duì)機(jī)制之前,咱們先來(lái)弄明白到底什么是請(qǐng)求的排隊(duì)機(jī)制,帶著這個(gè)問(wèn)題一起通過(guò)本文學(xué)習(xí)吧2024-07-07
nginx反向代理服務(wù)因配置文件錯(cuò)誤導(dǎo)致訪問(wèn)資源時(shí)出現(xiàn)404
這篇文章主要介紹了nginx反向代理服務(wù)因配置文件錯(cuò)誤導(dǎo)致訪問(wèn)資源時(shí)出現(xiàn)404,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
NGINX njs從基礎(chǔ)配置到高級(jí)特性實(shí)戰(zhàn)
njs是NGINX推出的輕量級(jí)JavaScript子集,通過(guò) ngx_http_js_module`模塊可在NGINX中實(shí)現(xiàn) location 處理、變量定義、響應(yīng)過(guò)濾等核心能力,感興趣的可以了解一下2026-01-01
nginx通過(guò)location配置代理的原理和實(shí)現(xiàn)方式
這篇文章主要介紹了nginx通過(guò)location配置代理的原理和實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
Nginx限流防刷與CC攻擊防護(hù)實(shí)戰(zhàn)配置
本文介紹了Nginx在Web服務(wù)中的防護(hù)方案,包括限流基礎(chǔ)、分場(chǎng)景限流、CC攻擊防護(hù)、IP黑白名單、監(jiān)控與告警以及壓測(cè)驗(yàn)證,感興趣的可以了解一下2026-01-01
nginx上部署react項(xiàng)目的實(shí)例方法
今天小編就為大家分享一篇關(guān)于nginx上部署react項(xiàng)目的實(shí)例方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02

