我想在代理后面提供我的 Restful API,但我不知道如何将请求重定向到 spring boot 应用程序以便可以通过域名访问它。
我的 spring boot 应用程序使用 spring-boot-starter-tomcat 运行,应用程序部署正常,我可以在服务器上使用 java -jar myApplication.jar 部署它。
也可以通过编写 https://1.2.3.4:8090 来远程访问该应用程序在浏览器上。
我使用 NGINX(版本:nginx/1.11.10)作为反向代理。这是我的配置:
nginx.conf
include /etc/nginx/modules.conf.d/*.conf;
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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalºº 0;
keepalive_timeout 65;
#tcp_nodelay on;
#gzip on;
#gzip_disable "MSIE [1-6]\.(?!.*SV1)";
server_tokens off;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
# override global parameters e.g. worker_rlimit_nofile
include /etc/nginx/*global_params;
sites-available/fakedomain.com
server {
listen 443 ssl;
server_name fakedomain.com;
ssl on;
ssl_certificate /../certificate.pem;
ssl_certificate_key /../certificate.key.pem;
ssl_session_cache shared:SSL:10m;
location /server/ {
proxy_redirect http://1.2.3.4:8090 https://fakedomain.com/;
proxy_pass_header Server;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_connect_timeout 5;
proxy_read_timeout 240;
proxy_intercept_errors on;
proxy_pass http://1.2.3.4:8090;
}
}
服务器响应状态代码:301 永久移动。
控制台输出为:
XMLHttpRequest cannot load https://www.fakedomain.com/api/v1/method. Redirect from 'https://www.fakedomain.com/api/v1/method' to 'https://fakedomain.com/api/v1/method' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fakedomain.com' is therefore not allowed access.
最佳答案
您只需要 proxy_pass
而不是 proxy_redirect
。
您已经在此行发送了重定向:
proxy_redirect http://1.2.3.4:8090 https://fakedomain.com/;
关于java - NGINX反向代理背后的SpringBoot API REST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43981427/