【问题标题】:SpringBoot API REST behind NGINX reverse proxyNGINX 反向代理背后的 Spring Boot API REST
【发布时间】:2017-10-14 08:28:06
【问题描述】:

我想在代理后面提供我的 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.

【问题讨论】:

    标签: java nginx spring-boot cors reverse-proxy


    【解决方案1】:

    您只需要proxy_pass 而不是proxy_redirect

    您已经在这一行发送了重定向:

    proxy_redirect          http://1.2.3.4:8090 https://fakedomain.com/;
    

    【讨论】:

      猜你喜欢
      • 2019-06-13
      • 2019-03-14
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 2022-06-23
      • 1970-01-01
      • 2012-02-04
      相关资源
      最近更新 更多