jkrlos
jkrlos

Reputation: 133

SpringBoot API REST behind NGINX reverse proxy

I want to offer my Restful API behind proxy and I don't know how to redirect request to a spring boot application so it's accessible with a domain name.

My spring boot application is running using spring-boot-starter-tomcat, the application deploys fine, I can deploy it using java -jar myApplication.jar on the server.

The application is also accessible remotely by writing https://1.2.3.4:8090 on a browser.

I use NGINX (version: nginx/1.11.10) as reverse proxy. This is my configuration:

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;
    }
}

Server responses with a Status Code:301 Moved Permanently.

And console output is:

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.

Upvotes: 3

Views: 6486

Answers (1)

user2964500
user2964500

Reputation:

You only need proxy_pass instead of proxy_redirect.

You have already sent the redirect on this line:

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

Upvotes: 0

Related Questions