Run Spring boot App using docker container and nginx - spring-boot

I trying to open JSP files using docker containers and nginx server. I run spring boot app by making it as a container. But when I try to open http://152.18.5.150:8081/hello page it cannot find root path.
My app/Dockerfile
FROM openjdk:8
ADD jarfolder/docker-spring-controller.jar docker-spring-controller.jar
EXPOSE 8081
ENTRYPOINT ["java", "-jar", "/docker-spring-controller.jar"]
docker-compose.yml
version: '3'
services:
nginx:
container_name: some-nginx-control
image: nginx:1.13
restart: always
ports:
- 80:80
- 443:443
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
depends_on:
- app
app:
restart: always
build: ./app
ports:
- "8081:8081"
db:
restart: always
container_name: docker-postgresql_2
image: postgres:9.6
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=postgres
- POSTGRES_DB=abdu
- PGDATA=/var/lib/postgresql/data/pgdata
ports:
- "5432:5432"
/nginx/conf.d/app.conf
server {
listen 80;
charset utf-8;
access_log off;
location ~ \.jsp$ {
root app/;
expires 30d;
}
location / {
proxy_pass http://app:8081;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /hello {
proxy_pass http://app:8081/hello;
}
location /static {
access_log off;
expires 30d;
alias /app/static;
}
}
application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.port=8081
MyController.java
#Controller
public class MyController {
#RequestMapping("/welcome")
public String welcome_page() { //ModelMap map
return "welcome";
}
#RequestMapping("/hello")
public String hello_page() { //ModelMap map
return "hello";
}
}
when i run command docker ps -a
IMAGE PORTS
nginx:1.13 0.0.0.0:80->80/tcp
docker-spring-controller_app 0.0.0.0:8081->8081/tcp
.jsp files are app/src/main/webapp/WEB-INF/jsp/
Please help me. Where I am making mistake

Related

Springboot docker-compose redis Cannot get Jedis connection;

This may seems as duplicate of docker-compose with springboot and redis and docker-compose redis connection issue but the solutions proposed there are not working in my case.
I am using springboot and the other services are unable to connect to redis service.
Below is my docker compose file:
version: "3.7"
services:
...
users:
build: ./users
ports:
- "8081:8081"
networks:
- jiji-microservices-network
depends_on:
- registry
- gateway
- redis_cache
- postgresDB
- rabbitmq
links:
- redis_cache
- postgresDB
- rabbitmq
environment:
# SPRING_CACHE_TYPE: redis
# SPRING_REDIS_HOST: redis_cache
# SPRING_REDIS_PORT: 6379
# SPRING_REDIS_PASSWORD:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgresDB:5432/jiji_users
SPRING_DATASOURCE_USERNAME: postgres
SPRING_DATASOURCE_PASSWORD: postgres
SPRING_JPA_HIBERNATE_DDL_AUTO: update
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://registry:8090/eureka
postgresDB:
image: postgres
restart: unless-stopped
ports:
- "5432:5432"
networks:
- jiji-microservices-network
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: jiji_users
redis_cache:
image: redis:latest
restart: on-failure
command: ["redis-server","--bind","redis_cache","--port","6379"]
ports:
- "6379:6379"
networks:
- jiji-microservices-network
rabbitmq:
image: rabbitmq:management
ports:
- "5672:5672"
- "15672:15672"
networks:
- jiji-microservices-network
networks:
jiji-microservices-network:
driver: bridge
Here below is my application.yml file:
...
cache:
type: redis
redis:
host: redis_cache
port: 6379
# cache-null-values: true
time-to-live: 2592000 #30 days
...
The error message I am getting:
users_1 | 2022-03-28 02:53:40.417 INFO 1 --- [nio-8081-exec-4] c.o.u.s.CustomAuthorizationFilter : /api/v1/users/getCode
users_1 | Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
docker ps shows that all the containers are up and running and that redis is listening on port 6379.
PS: It works fine without docker.
The issue was related to my Redisconfiguration. Fixed bit adding redis host and redis port to JedisConnectionFactory Bean.
#Configuration
#Slf4j
public class RedisConfiguration {
#Value("${spring.redis.host}")
private String REDIS_HOST;
#Value("${spring.redis.port}")
private Integer REDIS_PORT;
#Bean
public JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(REDIS_HOST, REDIS_PORT);
return new JedisConnectionFactory(config);
}
#Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setEnableTransactionSupport(true);
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
}

Serve springdoc swagger-ui in subdirectory nginx

I have read the docs about how to serve springdoc behind a reverse proxy (https://springdoc.org/index.html#how-can-i-deploy-springdoc-openapi-ui-behind-a-reverse-proxy) and also checked all of the questions about the topic but still having a problem.
I'm serving a spring-boot (2.6.2 version) application in a subdirectory with nginx:
location /subdirectory/ {
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Prefix "/subdirectory/";
proxy_set_header X-Forwarded-Host $http_x_forwarded_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-Protocol https;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Url-Scheme https;
}
I have also configured Spring Boot as per docs:
application.properties:
server.forward-headers-strategy=framework
server.tomcat.redirect-context-root=false
When I visit my domain for a normal request to the Spring Boot API works fine, e.g.:
GET https://example.com/subdirectory/countries - OK 200 Returns the expected JSON
But when I try to visit the springdoc swagger-ui with:
GET https://example.com/subdirectory/swagger-ui/index.html - OK 200
I get a 200 OK but the .css and .js files requests return 404. I see a blank page and nothing about swagger. Am I missing something?
I think you are missing forwarding filter.
#Bean
ForwardedHeaderFilter forwardedHeaderFilter() {
return new ForwardedHeaderFilter();
}

spring boot request.getRequestURL() cannot get https

My project is nginx+spring boot,
,spring boot is port 8080
,After I use nginx to forward, request.getRequestURL() can only get http but not https
public static String getRootUrl(HttpServletRequest request){
StringBuffer url = request.getRequestURL();
String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append(request.getSession().getServletContext().getContextPath()).append("/").toString();
return tempContextUrl;
}
nginx is configured with https
server {
listen 80;
server_name xx.xxx.xx;
add_header Strict-Transport-Security max-age=15768000;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name xx.xxx.xxx;
keepalive_timeout 10m;
ssl_certificate cert.pem; #
ssl_certificate_key cert.key; #
ssl_session_timeout 24h;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
add_header Content-Security-Policy upgrade-insecure-requests;
}
}
your nginx proxy to port 8080, this application is a integrated tomcat server, you not configure ssl in this tomcat , so, inside of the application, it is http, you can configure ssl in spring boot.

Running spring boot application behind nginx - missing location prefix when redirect

I have spring boot application running on URL: http://localhost:8080, with two endpoints:
/endpoint1
/login2 - login page for authentication
Then I run it behind proxy server - nginx running on port 81. In the nginx configuration when I put something like this:
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header Host $host;
proxy_pass http://localhost:8080/;
}
then everything works fine: when I use http://localhost:81/endpoint1 then I'm redirected to http://localhost:81/login2.
However I'd like my nginx application URL to look like http://localhost:81/my_prefix/login2. So I've added the prefix to nginx configuration:
location /my_prefix/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header Host $host;
proxy_pass http://localhost:8080/;
}
But when I try with URL: http://localhost:81/my_prefix/endpoint1 then I'm redirected to: http://localhost:81/login2 - without /my_prefix.
In my Spring WebSecurityConfigurerAdapter configuration class I have some security configuration with login page for authentication:
#Override
protected void configure(final HttpSecurity http) throws Exception {
http.csrf().disable()
.addFilterBefore(authenticationFilter(), UsernamePasswordAuthenticationFilter.class)
.authorizeRequests()
.antMatchers("/", "/metadata", "/favicon.ico", "/api/**", "/*.css", "/*.js").permitAll()
.antMatchers("/admin/**").hasRole("ADMIN")
.anyRequest().hasRole("USER").and()
.formLogin().loginPage("/login2").permitAll()
.and()
.logout().logoutSuccessUrl("/");
}
Also I have property: server.use-forward-headers: true
How can I force Spring MVC to include nginx location prefix (/my_prefix) when redirecting to login page?
You need to send a header X-Forwarded-Prefix: /my_prefix from your nginx and enable forward headers in Spring Boot application.yaml
server.forward-headers-strategy: FRAMEWORK
See Running Behind a Front-end Proxy Server
You need to change this:
proxy_pass http://localhost:8080/;
to this:
proxy_pass http://localhost:8080;

Nginx configuration for one more project which are running on Tomcat7

I have two projects which are running on Tomcat7.
http://localhost:8080/mysite1 and http://localhost:8080/mysite2
mysite1.com and mysite2.com should work on one server.
Example nginx configuration.
server {
server_name mysite1.com;
return 301 $scheme://www.mysite1.com$request_uri;
}
server {
server_name www.mysite1.com;
root /var/lib/tomcat7/webapps/mysite1;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080/;
}
}
server {
server_name mysite2.com;
return 301 $scheme://www.mysite2.com$request_uri;
}
server {
server_name www.mysite2.com;
root /var/lib/tomcat7/webapps/mysite2;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080/mysite2/;
}
}
Please, direct me.

Resources