찾다
운영 및 유지보수엔진스nginx를 사용하여 쿠키 교차 도메인 액세스 문제를 해결하는 방법

1. 앞에 작성

최근에는 Alibaba Cloud의 4개 서버 프로젝트를 고객이 제공한 새 프로젝트로 마이그레이션해야 합니다. 원래 4개 서버는 1차 도메인 이름과 2차 도메인 이름을 사용했습니다. 예를 들어 aaa.abc.com, bbb.abc.com 및 ccc.abc.com입니다. 그 중 aaa.abc.com은 쿠키의 정보를 .abc.com으로 설정하여 로그인합니다. 다른 시스템에서는 이 쿠키를 공유할 수 있습니다. 그러나 4개의 새로운 서버에는 도메인 이름이 적용되지 않고 4개의 IP만 있습니다:

192.168.0.1 Single Sign-On 서버

192.168.0.2

192.168.0.3

192.168.0.4

왜냐하면 각 서버에는 두 프로젝트 모두 싱글 사인온(Single Sign-On)을 사용하고 있어서 새로운 공유 로그인 방식을 수정하는데 시간이 너무 많이 걸려서 인터넷에서 쿠키 크로스 도메인 로그인을 검색해서 시도해보고 192.168.0.1 싱글 사인에서 도메인을 여러 번 설정했습니다. -서버 2, 3, 4개에서는 브라우저가 허용하지 않기 때문에 결과가 이상적이지 않습니다. 나중에 우연히 nginx가 속임수를 통해 쿠키를 공유할 수 있다는 것을 보았습니다. 그래서 원래 회사에서는 nginx를 배포하고 이런 용도를 갖고 있는 줄 알았습니다.

2. 원본 nginx 구성

먼저 nginx 설치에 대해 이야기하겠습니다. 인터넷에 많은 튜토리얼이 있으므로 Linux에서 nginx 설치 및 시작에 대해서는 자세히 설명하지 않겠습니다. 주목해야 할 것은 ./configure 이후의 다양한 with입니다. 구성 시작 프로세스 중에 몇 가지 문제가 발생했습니다:

nginx: [emerg] unknown directive "aio" in

plus --with-file-aio

코드 복사 코드는 다음과 같습니다:

nginx 시작 : nginx: [ emerg] inet6 소켓은 이 플랫폼에서

의 "[::]:80"에 지원되지 않습니다. --with-ipv6을 끝에 추가하면 더 쉽게 할 수 있습니다.

설치가 완료된 후. 주로 nginx.conf 구성

원래 서버 구성 nginx.conf:

# for more information on configuration, see:
#  * official english documentation: http://nginx.org/en/docs/
#  * official russian documentation: http://nginx.org/ru/docs/

user root;
worker_processes 2;
worker_cpu_affinity 1000 0100;
error_log logs/error.log;
pid logs/nginx.pid;


events {
  worker_connections 2048;
}

http {
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" '
           '"$http_user_agent" "$http_x_forwarded_for"';

  access_log logs/access.log main;

  gzip on;
  gzip_min_length 1000;
  gzip_buffers   4 8k;
  gzip_types    text/plain application/javascript application/x-javascript text/css application/xml;

  client_max_body_size 8m;
  client_body_buffer_size 128k;

  sendfile      on;
  tcp_nopush     on;
  tcp_nodelay     on;
  keepalive_timeout  65;
  types_hash_max_size 2048;

  include       mime.types;
  default_type    application/octet-stream;

  connection_pool_size 512;
  aio on;
  open_file_cache max=1000 inactive=20s;

  # load modular configuration files from the /etc/nginx/conf.d directory.
  # see http://nginx.org/en/docs/ngx_core_module.html#include
  # for more information.
  #  主要配置在这里,nginx.conf配置都是一样
  include /usr/local/nginx/conf/conf.d/*.conf;

  server {
    listen    80 default_server;
    listen [::]:80 ipv6only=on default_server;
    server_name _;
    root     html;

    # load configuration files for the default server block.
    include /usr/local/nginx/conf/default.d/*.conf;

    location / {
    }

    error_page 404 /404.html;
      location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
      location = /50x.html {
    }
  }
}

원래 서버
conf.d/*.conf 구성은 reverse-proxy.conf

server
{
  listen 80;
  server_name m.abc.com.cn;
  location / {
    root  /usr/share/nginx/html/;
    index index.html index.htm;
  }
  location ~ \.(jsp|do)?$ {
    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_pass http://localhost:8084;
  }
  if ($http_user_agent ~* "qihoobot|baiduspider|googlebot|googlebot-mobile|googlebot-image|mediapartners-google|adsbot-google|feedfetcher-google|yahoo! slurp|yahoo! slurp china|youdaobot|sosospider|sogou spider|sogou web spider|msnbot|ia_archiver|tomato bot") { 
        return 403; 
    }
  access_log /home/logs/nginx/m.abc.com.cn_access.log;
}
 
server
{
  listen 80;
  server_name store.abc.com.cn *.store.abc.com.cn;
  location / {
    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_pass http://localhost:8081;
  }
  access_log /home/logs/nginx/store.abc.com.cn_access.log;
}

server
{
  listen 80;
  server_name shopcenter.abc.com.cn;
  location / {
    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_pass http://10.45.100.222:8082;
  }
  access_log /home/logs/nginx/shopcenter.abc.com.cn_access.log;
}
 
server
{
  listen 80;
  server_name search.abc.com.cn;
  location / {
    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_pass http://10.45.100.68:8083;
  }
  access_log /home/logs/nginx/search.abc.com.cn_access.log;
}

위 구성 후 nginx가 시작된 후, 다른 도메인 이름에 액세스하여 다른 서버에 액세스하세요. 그리고 모두 2차 도메인 이름인 .abc.com.cn을 갖고 있기 때문입니다. 따라서 쿠키를 공유할 수 있습니다.

nginx 파일 구조는 다음과 같습니다.

nginx를 사용하여 쿠키 교차 도메인 액세스 문제를 해결하는 방법

3. 수정된 nginx 구성

주로 reverse-proxy.conf가 다릅니다

server
{
  listen 9998;
  server_name 192.168.0.1:9998;
  location /servlets/ {
    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_pass http://192.168.0.1:8088;
  }
  location / {

    root  /usr/local/nginx/html/web/;
    index index.html index.htm;
  }
  location ~ \.(jsp|do)?$ {
    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_pass http://192.168.0.1:8088;
    
    proxy_http_version 1.1;
    proxy_set_header upgrade $http_upgrade;
    proxy_set_header connection "upgrade";
    proxy_read_timeout  700s;
  } 
if ($http_user_agent ~* "qihoobot|baiduspider|googlebot|googlebot-mobile|googlebot-image|mediapartners-google|adsbot-google|feedfetcher-google|yahoo! slurp|yahoo! slurp china|youdaobot|sosospider|sogou spider|sogou web spider|msnbot|ia_archiver|tomato bot") { 
        return 403; 
    }
  access_log /usr/local/nginx/logs/www.abc.com.cn_access.log;
}

server
{
  listen 9994;
  server_name 192.168.0.1:9994;
  location / {
   proxy_redirect off;

    root  /usr/local/nginx/html/weixin/;
    index index.html index.htm;
  }
  location ~ \.(jsp|do)?$ {
    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_pass http://localhost:8084;
  }
  if ($http_user_agent ~* "qihoobot|baiduspider|googlebot|googlebot-mobile|googlebot-image|mediapartners-google|adsbot-google|feedfetcher-google|yahoo! slurp|yahoo! slurp china|youdaobot|sosospider|sogou spider|sogou web spider|msnbot|ia_archiver|tomato bot") { 
        return 403; 
    }
  access_log /usr/local/nginx/logs/m.abc.com.cn_access.log;
}
 
server
{
  listen 9990;
  server_name store.abc.com.cn *.store.abc.com.cn;
  location / {
    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_pass http://localhost:8081;
  }
  access_log /usr/local/nginx/logs/store.abc.com.cn_access.log;
}

server
{
  listen 9992;
  server_name 192.168.0.1:9992;
  location / {
    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_pass http://192.168.0.2:8082;
  }
  access_log /usr/local/nginx/logs/shopcenter.abc.com.cn_access.log;
}
 
server
{
  listen 9993;
  server_name 192.168.0.1:9993;
  location / {
    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_pass http://192.168.0.3:8083;
  }
  access_log /usr/local/nginx/logs/search.abc.com.cn_access.log;
}

이런 식으로 192.168.0.1:9998을 단일 포인트 서버로 사용할 수 있으며 로그인 다음 도메인은 모두 192.168.0.1입니다. 나머지 0.2와 0.3은 192.168.0.1nginx라는 서로 다른 포트와 단일 지점 서버를 통해 접근이 가능하므로 0.1이라는 도메인 이름을 공유할 수 있다.

위 내용은 nginx를 사용하여 쿠키 교차 도메인 액세스 문제를 해결하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
이 기사는 亿速云에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제
Nginx vs. Apache : 성능, 확장 성 및 효율성Nginx vs. Apache : 성능, 확장 성 및 효율성Apr 19, 2025 am 12:05 AM

Nginx와 Apache는 성능, 확장 성 및 효율성 측면에서 고유 한 장점과 단점을 가진 강력한 웹 서버입니다. 1) NGINX는 정적 컨텐츠를 처리하고 역전 프록시를 처리 할 때 잘 수행되며 동시 동시성 시나리오에 적합합니다. 2) Apache는 동적 컨텐츠를 처리 할 때 더 나은 성능을 발휘하며 풍부한 모듈 지원이 필요한 프로젝트에 적합합니다. 서버 선택은 프로젝트 요구 사항 및 시나리오에 따라 결정해야합니다.

궁극적 인 대결 : Nginx vs. Apache궁극적 인 대결 : Nginx vs. ApacheApr 18, 2025 am 12:02 AM

Nginx는 높은 동시 요청을 처리하는 데 적합한 반면 Apache는 복잡한 구성 및 기능 확장이 필요한 시나리오에 적합합니다. 1.NGINX는 이벤트 중심의 비 블로킹 아키텍처를 채택하며, 대결 환경에 적합합니다. 2. Apache는 프로세스 또는 스레드 모델을 채택하여 복잡한 구성 요구에 적합한 풍부한 모듈 생태계를 제공합니다.

NGINX의 행동 : 예제 및 실제 응용 프로그램NGINX의 행동 : 예제 및 실제 응용 프로그램Apr 17, 2025 am 12:18 AM

Nginx는 웹 사이트 성능, 보안 및 확장 성을 향상시키는 데 사용될 수 있습니다. 1) 리버스 프록시 및로드 밸런서로서 Nginx는 백엔드 서비스를 최적화하고 트래픽을 공유 할 수 있습니다. 2) 이벤트 중심 및 비동기 아키텍처를 통해 Nginx는 높은 동시 연결을 효율적으로 처리합니다. 3) 구성 파일을 사용하면 정적 파일 서비스 및로드 밸런싱과 같은 규칙을 유연하게 정의 할 수 있습니다. 4) 최적화 제안에는 GZIP 압축 활성화, 캐시 사용 및 작업자 프로세스 조정이 포함됩니다.

NGINX 장치 : 다양한 프로그래밍 언어를 지원합니다NGINX 장치 : 다양한 프로그래밍 언어를 지원합니다Apr 16, 2025 am 12:15 AM

NginxUnit은 여러 프로그래밍 언어를 지원하며 모듈 식 디자인을 통해 구현됩니다. 1. 언어 모듈로드 : 구성 파일에 따라 해당 모듈을로드합니다. 2. 응용 프로그램 시작 : 호출 언어가 실행될 때 응용 프로그램 코드를 실행합니다. 3. 요청 처리 : 응용 프로그램 인스턴스로 요청을 전달하십시오. 4. 응답 반환 : 처리 된 응답을 클라이언트에 반환합니다.

nginx와 apache 사이의 선택 : 필요에 맞는 적합nginx와 apache 사이의 선택 : 필요에 맞는 적합Apr 15, 2025 am 12:04 AM

Nginx와 Apache는 고유 한 장점과 단점이 있으며 다른 시나리오에 적합합니다. 1.NGINX는 높은 동시성 및 낮은 자원 소비 시나리오에 적합합니다. 2. Apache는 복잡한 구성 및 풍부한 모듈이 필요한 시나리오에 적합합니다. 핵심 기능, 성능 차이 및 모범 사례를 비교하면 요구에 가장 적합한 서버 소프트웨어를 선택할 수 있습니다.

nginx를 시작하는 방법nginx를 시작하는 방법Apr 14, 2025 pm 01:06 PM

질문 : nginx를 시작하는 방법? 답변 : nginx 스타트 업 설치 nginx verification nginx is nginx 시작 다른 시작 옵션을 자동으로 시작합니다.

nginx가 시작되었는지 확인하는 방법nginx가 시작되었는지 확인하는 방법Apr 14, 2025 pm 01:03 PM

nginx가 시작되었는지 확인하는 방법 : 1. 명령 줄을 사용하십시오 : SystemCTL 상태 nginx (linux/unix), netstat -ano | Findstr 80 (Windows); 2. 포트 80이 열려 있는지 확인하십시오. 3. 시스템 로그에서 nginx 시작 메시지를 확인하십시오. 4. Nagios, Zabbix 및 Icinga와 같은 타사 도구를 사용하십시오.

nginx를 닫는 방법nginx를 닫는 방법Apr 14, 2025 pm 01:00 PM

Nginx 서비스를 종료하려면 다음 단계를 따르려면 다음 단계를 결정합니다. Red Hat/Centos (SystemCTL 상태 NGINX) 또는 Debian/Ubuntu (서비스 NGINX 상태) 서비스 중지 : Red Hat/Centos (SystemCTL STOP NGINX) 또는 DEBIAN/UBUNTU (서비스 NGINX STOP) DIA AUTAL STARTUP (옵션) : RED HAT/CENTOS (SystemCTLED) 또는 DEBIAN/UBUNT (SystemCTLED). (Syst

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

mPDF

mPDF

mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

SublimeText3 영어 버전

SublimeText3 영어 버전

권장 사항: Win 버전, 코드 프롬프트 지원!

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기