簡體   English   中英

NGBX反向代理背后的SpringBoot API REST

[英]SpringBoot API REST behind NGINX reverse proxy

我想在代理后面提供我的Restful API,我不知道如何將請求重定向到Spring引導應用程序,因此可以使用域名訪問它。

我的spring啟動應用程序使用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;

網站可用/ 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/;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM