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

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

May 26, 2023 pm 11:21 PM
nginx cookie

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으로 문의하세요.

핫 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를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 채팅 명령 및 사용 방법
1 몇 달 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

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

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

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와 같은 타사 도구를 사용하십시오.

Windows에서 nginx를 구성하는 방법 Windows에서 nginx를 구성하는 방법 Apr 14, 2025 pm 12:57 PM

Windows에서 Nginx를 구성하는 방법은 무엇입니까? nginx를 설치하고 가상 호스트 구성을 만듭니다. 기본 구성 파일을 수정하고 가상 호스트 구성을 포함하십시오. 시작 또는 새로 고침 Nginx. 구성을 테스트하고 웹 사이트를보십시오. SSL을 선택적으로 활성화하고 SSL 인증서를 구성하십시오. 포트 80 및 443 트래픽을 허용하도록 방화벽을 선택적으로 설정하십시오.

nginx가 시작되었는지 확인하는 방법은 무엇입니까? nginx가 시작되었는지 확인하는 방법은 무엇입니까? Apr 14, 2025 pm 12:48 PM

Linux에서는 다음 명령을 사용하여 nginx가 시작되었는지 확인하십시오. SystemCTL 상태 Nginx 판사 명령 출력에 따라 : "active : running"이 표시되면 Nginx가 시작됩니다. "Active : 비활성 (죽음)"이 표시되면 Nginx가 중지됩니다.

Linux에서 Nginx를 시작하는 방법 Linux에서 Nginx를 시작하는 방법 Apr 14, 2025 pm 12:51 PM

Linux에서 Nginx를 시작하는 단계 : Nginx가 설치되어 있는지 확인하십시오. systemctl start nginx를 사용하여 nginx 서비스를 시작하십시오. SystemCTL을 사용하여 NGINX를 사용하여 시스템 시작시 NGINX의 자동 시작을 활성화하십시오. SystemCTL 상태 nginx를 사용하여 시작이 성공했는지 확인하십시오. 기본 환영 페이지를 보려면 웹 브라우저의 http : // localhost를 방문하십시오.

nginx 서버를 시작하는 방법 nginx 서버를 시작하는 방법 Apr 14, 2025 pm 12:27 PM

Nginx 서버를 시작하려면 다른 운영 체제에 따라 다른 단계가 필요합니다. Linux/Unix System : Nginx 패키지 설치 (예 : APT-Get 또는 Yum 사용). SystemCTL을 사용하여 nginx 서비스를 시작하십시오 (예 : Sudo SystemCtl start nginx). Windows 시스템 : Windows 바이너리 파일을 다운로드하여 설치합니다. nginx.exe 실행 파일을 사용하여 nginx를 시작하십시오 (예 : nginx.exe -c conf \ nginx.conf). 어떤 운영 체제를 사용하든 서버 IP에 액세스 할 수 있습니다.

nginx403 오류를 해결하는 방법 nginx403 오류를 해결하는 방법 Apr 14, 2025 pm 12:54 PM

서버는 요청 된 리소스에 액세스 할 수있는 권한이 없으므로 Nginx 403 오류가 발생합니다. 솔루션에는 다음이 포함됩니다. 파일 권한 확인 권한을 확인하십시오. .htaccess 구성을 확인하십시오. nginx 구성을 확인하십시오. Selinux 권한을 구성하십시오. 방화벽 규칙을 확인하십시오. 브라우저 문제, 서버 장애 또는 기타 가능한 오류와 같은 다른 원인을 해결하십시오.

Nginx 크로스 도메인의 문제를 해결하는 방법 Nginx 크로스 도메인의 문제를 해결하는 방법 Apr 14, 2025 am 10:15 AM

Nginx 크로스 도메인 문제를 해결하는 두 가지 방법이 있습니다. 크로스 도메인 응답 헤더 수정 : 교차 도메인 요청을 허용하고 허용 된 메소드 및 헤더를 지정하고 캐시 시간을 설정하는 지시문을 추가하십시오. CORS 모듈 사용 : 모듈을 활성화하고 CORS 규칙을 구성하여 크로스 도메인 요청, 메소드, 헤더 및 캐시 시간을 허용합니다.

Nginx403을 해결하는 방법 Nginx403을 해결하는 방법 Apr 14, 2025 am 10:33 AM

Nginx 403 금지 된 오류를 수정하는 방법은 무엇입니까? 파일 또는 디렉토리 권한을 확인합니다. 2. 확인 파일을 확인하십시오. 3. nginx 구성 파일 확인; 4. nginx를 다시 시작하십시오. 다른 가능한 원인으로는 방화벽 규칙, Selinux 설정 또는 응용 프로그램 문제가 있습니다.

See all articles