PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 1, 2022

[FIXED] How to deploy backend and frontend on the same server but different path with Nginx

 September 01, 2022     nginx, nginx-config, nginx-location, nginx-reverse-proxy, spring-boot     No comments   

Issue

I need to deploy the frontend in https://service.domain.it and the backend in https://service.domain.it/api. I've done a deploy configuration but I'm having troube with spring security + JWT Authentication. This is the configure method of WebSecurityConfigurerAdapter

@Override
public void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
            .authorizeRequests()
            .antMatchers("/public/**").permitAll() //login/logout/recoverypassw
            .antMatchers("/user/**").hasAnyRole("USER","ADMIN")
            .antMatchers("/admin/**").hasRole("ADMIN")
            .anyRequest().authenticated()
            .and().exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).
            and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).
            and().addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}

And this is the nginx configuration

#service.domain.it
server{
        listen 443 ssl;
        server_name service.domain.it;
        ssl_certificate /etc/letsencrypt/live/service.domain.it/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/service.domain.it/privkey.pem;
        location /api/ {
                #backend
                proxy_pass http://localhost:8085;
        }

       location / {
               #frontend
               index index.html;
                alias /var/www/html/Service/;
       }
}
server{
        listen 80;
        server_name service.domain.it;
        return 301 https://service.domain.it$request_uri;
}

What should I do?


Solution

Your backend service does not understand the /api used in your proxy_configuration. You must rewrite the URI before sending it to the backend.

location /api {

   rewrite ^/api/(.*) /$1 break;
   proxy_pass http://backend/;

}

This will remove the /api from the URI before sending it to the backend.



Answered By - Timo Stark
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing