
여러 서비스가 포함된 도메인이 있는 개발 상황이 있습니다.
https://somewebpage.com
이 서비스에는 하위 디렉터리로 여러 프로젝트가 있습니다.
https://somewebpage.com
<- 방문 페이지https://somewebpage.com/api
<- 나머지 API 서버https://somewebpage.com/app
<- 내 앱
https://somewebpage.com/app
그렇다면 nginx와 호스트 파일을 내 로컬 빌드에만 역방향 프록시로 설정하는 것이 가능합니까(그리고 어떻게) http://localhost:3000
?
문제는 앱이 배포될 때 /api
나머지 서버에 액세스하는 데 문제가 없지만 로컬로 제공할 때 내 nginx 역방향 프록시 가로채기 landing page
및 rest api server
URL도 있다는 것입니다.
내 nginx 구성은 다음과 같습니다.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
index index.html;
proxy_max_temp_file_size 0;
proxy_buffering off;
server {
listen 80;
server_name somewebpage.com;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name somewebpage.com;
ssl_certificate /etc/ssl/certs/certificate.crt;
ssl_certificate_key /etc/ssl/certs/ccertificate.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
location /app {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_pass http://localhost:3000;
}
}
}
그리고 내 안에는 /etc/hosts
:
127.0.0.1 somewebpage.com
비슷한 결과를 얻는 방법에 대한 다른 트릭이 있습니까?
내가 이 작업을 시도하는 이유는 내가 이 작업을 수행하면 오류 localhost:3000
로 응답 CORS
하고 에 대한 호출을 거부하기 때문입니다 /api
.
아니면 보안 위험이 너무 높아서 다른 액세스 방법을 요청해야 합니까 /api
?
미리 답변해 주셔서 감사합니다.
답변1
다음을 추가해야 합니다.
location / {
try_files $uri $uri/ =404;
}
이는 nginx에게 지정된 다른 요청과 일치하지 않는 요청을 처리하는 방법을 알려줍니다 location
. nginx가 location
사용할 블록을 선택하는 방법에 대한 자세한 내용은 다음을 참조하세요.nginx 문서.
답변2
나는 나에게 맞는 하나의 솔루션을 생각해 냈습니다.
somewebpage.com
고정 IP 주소를 가리키 xx.xx.xx.xx
므로 Tero Kilkanen 답변과 유사한 URL 대신 해당 IP 주소에 또 다른 프록시를 추가했습니다.
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_pass https://xx.xx.xx.xx;
}
이렇게 하면 요청이 도메인 확인을 우회하므로 /etc/hosts
파일이 내 요청을 가로채지 않습니다 .somewebpage.com
그래서 결국 나는 나에게 도움이 되는 다음 nginx 구성으로 끝났습니다.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
index index.html;
proxy_max_temp_file_size 0;
proxy_buffering off;
server {
listen 80;
server_name somewebpage.com;
location / {
return 301 https://$host$request_uri;
}
}
server {
listen 443 ssl;
server_name somewebpage.com;
ssl_certificate /etc/ssl/certs/certificate.crt;
ssl_certificate_key /etc/ssl/certs/ccertificate.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
location /app {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_pass http://localhost:3000;
}
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_pass https://xx.xx.xx.xx;
}
}
}
이 솔루션은 장면 뒤의 여러 IP 주소, 동적 IP 주소 등으로 인해 모든 사람에게 적용되지 않을 수 있습니다. 그러나 그것은 나에게 효과적이었고 개발 목적으로는 충분했습니다.