>  기사  >  운영 및 유지보수  >  Nginx 프록시 Axios 요청 및 주의사항은 무엇입니까?

Nginx 프록시 Axios 요청 및 주의사항은 무엇입니까?

WBOY
WBOY앞으로
2023-05-15 17:43:061008검색

1. nginx.conf 구성 정보

nginx.conf에는 많은 구성 정보가 있으므로 이 문서에서는 axios 및 정적 리소스 요청 설정에만 중점을 두고 몇 가지 일반적인 구성 항목도 언급합니다. 구체적인 설정은 다음과 같습니다.

# 设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
 #连接超时时间
 keepalive_timeout 120;
 
 #gzip压缩开关及相关配置
 gzip on;
 gzip_min_length 1k;
 gzip_buffers  4 32k;
 gzip_http_version 1.1;
 gzip_comp_level 2;
 gzip_types  text/plain application/x-javascript text/css application/xml;
 gzip_vary on;
 gzip_disable "msie [1-6].";

 #设定实际的服务器列表 
 upstream zp_server{
 server 127.0.0.1:8089;
 }
 
 #http服务器 
 server {
 #监听80端口
 listen 80
 
 #定义服务名称
 server_name localthost;
 
 #首页
 index index.html
 
 #指向项目根目录
 root d:\project\src\main\webapp;
 
 #编码格式
 charset utf-8;
 
 #代理的路径(和upstream绑定),location 后面设置映射的路径
 location / {
  #代理配置参数
  proxy_connect_timeout 180;
  proxy_send_timeout 180;
  proxy_read_timeout 180;
  proxy_set_header host $host;
  proxy_set_header x-forwarder-for $remote_addr;
  proxy_pass http://zp_server/;
  
  #跨域相关设置
  add_header 'access-control-allow-origin' '*' always;
  add_header 'access-control-allow-credentials' 'true';
  add_header 'access-control-allow-headers' 'origin, x-requested-with, content-type, accept' always;
  }
  
  #配置静态资源 解决js css文件无法加载无法访问的问题,注意末尾不能有 /
  location ~ .*\.(js|css|jpg|png)$ {
  proxy_pass http://zp_server;
  } 
 }
}

2. Proxy_pass의 슬래시 문제

nginx는 Proxy_pass를 두 가지 유형으로 나눕니다.

  • 하나에는 IP와 포트 번호만 포함됩니다(포트 뒤의 /도 포함됩니다). ) 아니요, 여기서는 특별한 주의를 기울여야 합니다. 예를 들어, proxy_pass http://localhost:8080 이 방법은 uri 없는 방법이라고 합니다.

  • 다른 하나는 포트 번호 뒤에 단일 경로만 포함하여 다른 경로가 있습니다. / , 예: Proxy_pass http://localhost:8080/ 및 기타 경로(예: Proxy_pass http://localhost:8080/abc).

2.1 uri가 없는 메소드의 경우

uri가 없는 메소드의 경우 nginx는 다음과 같은 위치에 경로 부분을 유지합니다.

location /api1/ {
 proxy_pass http://localhost:8080;
}

http://localhost/api1/xxx에 액세스하면 프록시는 http://localhost:8080/api1/xxx

2.2로 이동합니다. uri 메소드의 경우

uri 메소드의 경우 nginx는 별칭과 같은 대체 메소드를 사용하여 URL을 대체하며 이 대체는 단지 리터럴 대체, 예:

location /api2/ {
 proxy_pass http://localhost:8080/;
}

http://localhost/api2/xxx에 액세스할 때 http://localhost/api2/(끝의 / 참고)는 http://localhost:8080/으로 대체됩니다. 그런 다음 추가하면 나머지 xxx가 업로드되므로 http://localhost:8080/xxx가 됩니다.

2.3 요약

server {
 listen    80;
 server_name localhost;

 location /api1/ {
  proxy_pass http://localhost:8080;
 }
 # http://localhost/api1/xxx -> http://localhost:8080/api1/xxx


 location /api2/ {
  proxy_pass http://localhost:8080/;
 }
 # http://localhost/api2/xxx -> http://localhost:8080/xxx


 location /api3 {
  proxy_pass http://localhost:8080;
 }
 # http://localhost/api3/xxx -> http://localhost:8080/api3/xxx


 location /api4 {
  proxy_pass http://localhost:8080/;
 }
 # http://localhost/api4/xxx -> http://localhost:8080//xxx,请注意这里的双斜线,好好分析一下。


 location /api5/ {
  proxy_pass http://localhost:8080/haha;
 }
 # http://localhost/api5/xxx -> http://localhost:8080/hahaxxx,请注意这里的haha和xxx之间没有斜杠,分析一下原因。

 location /api6/ {
  proxy_pass http://localhost:8080/haha/;
 }
 # http://localhost/api6/xxx -> http://localhost:8080/haha/xxx

 location /api7 {
  proxy_pass http://localhost:8080/haha;
 }
 # http://localhost/api7/xxx -> http://localhost:8080/haha/xxx

 location /api8 {
  proxy_pass http://localhost:8080/haha/;
 }
 # http://localhost/api8/xxx -> http://localhost:8080/haha//xxx,请注意这里的双斜杠。
}

위 내용은 Nginx 프록시 Axios 요청 및 주의사항은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제