場(chǎng)景說(shuō)明:
前后端分離版的項(xiàng)目,微信小程序發(fā)布后,向后臺(tái)請(qǐng)求數(shù)據(jù)時(shí)訪(fǎng)問(wèn)后臺(tái)地址失敗,后臺(tái)地址需要https以及域名,不能直接使用ip地址,因此需要做java后臺(tái)地址的域名映射。
解決方案:
1、前置準(zhǔn)備:域名申請(qǐng)和備案,以及安全證書(shū)(此處不進(jìn)行贅述,網(wǎng)上有很多例子)
2、nginx配置(注意看注釋說(shuō)明)
【http://localhost:8075/】是java后臺(tái)提供的端口號(hào),該地址是最終所有ip的指向(前端請(qǐng)求后臺(tái)數(shù)據(jù)需要用的ip)
域名:xxx.xxxxx.net文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-694217.html
#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;
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#PC端網(wǎng)頁(yè)域名映射ssl認(rèn)證,網(wǎng)站地址欄可以輸入:https://xxx.xxxxx.net:8077 / 進(jìn)行網(wǎng)站訪(fǎng)問(wèn)
server {
listen 8077 ssl;
server_name xxx.xxxxx.net; #域名
#安全證書(shū)配置
ssl_certificate D:\xxx\key\xxx.pem;
ssl_certificate_key D:\xxx\key\xxx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root work/xxx; #前端資源
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /prod-api/ {
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;
#此處地址需要替換成被代理后的地址(https)
proxy_pass https://localhost:8074 /;
}
}
#域名映射java后臺(tái)提供的地址,微信小程序進(jìn)行訪(fǎng)問(wèn)該域名,拿到后臺(tái)數(shù)據(jù)
server {
#443或80,既訪(fǎng)問(wèn)該域名默認(rèn)的端口號(hào),
#原因:在微信小程序設(shè)置請(qǐng)求接口的域名(服務(wù)器域名設(shè)置)時(shí),不能加端口號(hào)
#因此:小程序訪(fǎng)問(wèn)后臺(tái)的地址直接填寫(xiě)該域名(xxx.xxxxx.net)不加端口號(hào),與小程序設(shè)置要求一致
listen 443 ssl;
server_name xxx.xxxxx.net; #域名
#安全證書(shū)配置
ssl_certificate D:\xxx\key\xxx.pem;
ssl_certificate_key D:\xxx\key\xxx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
#注意https
proxy_pass https://localhost:8074 /;
}
}
#代理后臺(tái)地址
server {
listen 8074 ssl;
server_name xxx.xxxxx.net; #域名
#安全證書(shū)
ssl_certificate D:\xxx\key\xxx.pem;
ssl_certificate_key D:\xxx\key\xxx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
#后臺(tái)地址
proxy_pass http://localhost:8075/;
}
}
}
3、注意事項(xiàng),服務(wù)器需開(kāi)放相關(guān)端口號(hào)允許外部訪(fǎng)問(wèn),如配置文檔中的8077、443端口號(hào)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-694217.html
到了這里,關(guān)于nginx域名配置——微信小程序訪(fǎng)問(wèn)后臺(tái)地址的域名配置的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!