컴퓨터 프로그래밍/Memo

[Memo] NGINX 사용을 위한 yml 세팅

한33 2024. 11. 10. 18:21

무중단 배포를 위해서 NGINX 를 사용하기 위해 yml 세팅을 해줬다.

 

소셜로그인 redirect url 설정을 위해서

 

application.yml

kakao:
  redirect-uri: "http://localhost:8080/ssaktium/signin/kakao"
google:
  redirect-uri: "http://localhost:8080/ssaktium/signin/google"
naver:
  redirect-uri: "http://localhost:8080/ssaktium/signin-naver"

 

기존에 위처럼 설정해줬었는데

 

배포를 하면 localhost 가 아니라 탄력적 ip 가 들어가야하기 때문에

 

설정을 추가해줘야했다.

 

spring:
  profiles:
    active: local
    group:
      local: local, common
      blue: blue, common
      green: green, common

server:
  env: blue

 

setting 을 먼저 위처럼 local, blue, green 으로 놨고 group 으로 묶어서 local 이든 blue 든 green 이든 common setting 은 포함하도록 하였다.

 

---
spring:
  config:
    activate:
      on-profile: local
  security:
    oauth2:
      client:
        registration:
          kakao:
            redirect-uri: "http://localhost:8080/ssaktium/signin/kakao"
          google:
            redirect-uri: "http://localhost:8080/ssaktium/signin/google"
          naver:
            redirect-uri: "http://localhost:8080/ssaktium/signin-naver"
server:
  port: 8080
  serverAddress: localhost
serverName: local_server

---
spring:
  config:
    activate:
      on-profile: blue
  security:
    oauth2:
      client:
        registration:
          kakao:
            redirect-uri: "http://13.124.138.199/ssaktium/signin/kakao"
          google:
            redirect-uri: "http://13.124.138.199/ssaktium/signin/google"
          naver:
            redirect-uri: "http://13.124.138.199/ssaktium/signin-naver"
server:
  port: 8080
  serverAddress: 13.124.138.199
serverName: blue_server

---
spring:
  config:
    activate:
      on-profile: green
  security:
    oauth2:
      client:
        registration:
          kakao:
            redirect-uri: "http://13.124.138.199/ssaktium/signin/kakao"
          google:
            redirect-uri: "http://13.124.138.199/ssaktium/signin/google"
          naver:
            redirect-uri: "http://13.124.138.199/ssaktium/signin-naver"
server:
  port: 8081
  serverAddress: 13.124.138.199
serverName: green_server
---

 

이렇게 local 일때, blue 일 때, green 일 때의 port 와 서버 이름, redirect url 을 설정해줬다.

 

spring:
  web:
    resources:
      static-locations: classpath:/static/
  config:
    activate:
      on-profile: common
    import: optional:file:.env[.properties]

 

그리고 아래 기존의 setting 에서는 redirect url 을 지웠고 on-profile 에 common 으로 등록해줬다.


 

@RestController
public class HealthCheckController {

    @Value("${server.env}")
    private String env;

    @Value("${server.port}")
    private String serverPort;

    @Value("${server.serverAddress}")
    private String serverAddress;

    @Value("${serverName}")
    private String serverName;

    @GetMapping("/hc")
    public ResponseEntity<CommonResponse<?>> healthCheck() {
        Map<String, String> responseData = new TreeMap<>();
        responseData.put("serverName", serverName);
        responseData.put("serverAddress", serverAddress);
        responseData.put("serverPort", serverPort);
        responseData.put("env", env);

        return ResponseEntity.ok(CommonResponse.success(responseData));
    }

    @GetMapping("/env")
    public ResponseEntity<CommonResponse<?>> getEnv() {
        return ResponseEntity.ok(CommonResponse.success(env));
    }
}

 

지금 어느 서버인지 확인하기 위해서 healthCheck 를 임시로 만들었다.

 

yml 파일의 데이터를 확인하기 위해서 가져와서 변수로 만들어줬고,

 

TreeMap 을 이용해서 이를 출력시켰다.

 

        "/hc",
        "/env"
).permitAll()
.anyRequest().authenticated()

 

Security Config 에서 이 url 을 열어주면 확인이 된다.

 

말고도 이후에 이메일 인증 등 코드에서 localhost:8080 이 들어가있는 부분이 있다면 변수처리를 위에서 해서 가져오면 된다.

 

위에서 변수를 선언해준다음에,

"http://" + ("localhost".equals(serverAddress) ? "localhost:8080" : serverAddress)

 

이런식으로 삼항연산자를 활용하면 된다.