Nginx 서버용 역방향 프록시를 구축하는 방법

WBOY
풀어 주다: 2023-05-15 08:40:05
앞으로
1654명이 탐색했습니다.

1부: 설치

1 사용자 및 그룹 만들기

  /usr/sbin/groupadd www 
  /usr/sbin/useradd -g www www
로그인 후 복사

2 pcre를 설치하여 향후 요구 사항에 맞게 nginx 역방향 프록시 지원 재작성

  wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz 
  tar zxvf pcre-7.8.tar.gz 
  cd pcre-7.8/ 
  ./configure 
  make && make install
로그인 후 복사

3 nginx 역방향 프록시 설치

  wget http://sysoev.ru/nginx/nginx-0.7.58.tar.gz 
  tar zxvf nginx-0.7.58.tar.gz 
  cd nginx-0.7.58/ 
  ./configure --user=www --group=www --prefix=/usr/
  local/webserver/nginx --with-http_stub_status_module 
  --with-http_ssl_module --with-cc-opt='-o2' --with-cpu-opt
  =opteron 
  make && make install
로그인 후 복사

주의 --with- 기사의 cc-opt='-o2' --with-cpu-opt=opteron은 컴파일러 최적화로, 현재 가장 많이 사용되는 것은 3이 아닌 -02이다. 다음은 CPU 모델에 해당한다.

2부: 구성 파일 구성 및 최적화

1 nginx.conf 구성 파일:

  user www www; 
  worker_processes 4; 
  # [ debug | info | notice | warn | error | crit ] 
  error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; 
  pid /usr/local/webserver/nginx/nginx.pid; 
  #specifies the value for maximum file descriptors that 
  can be opened by this process. 
  worker_rlimit_nofile 51200; 
  events 
  { 
  use epoll; 
  worker_connections 51200; 
  } 
  http 
  { 
  include mime.types; 
  default_type application/octet-stream; 
  source_charset gb2312; 
  server_names_hash_bucket_size 256; 
  client_header_buffer_size 256k; 
  large_client_header_buffers 4 256k; 
  #size limits 
  client_max_body_size 50m; 
  client_body_buffer_size 256k; 
  client_header_timeout 3m; 
  client_body_timeout 3m; 
  send_timeout 3m; 
  #参数都有所调整.目的是解决代理过程中出现的一些502 499错误  
  sendfile on; 
  tcp_nopush on; 
  keepalive_timeout 120; #参数加大,以解决做代理时502错误 
  tcp_nodelay on; 
  include vhosts/upstream.conf; 
  include vhosts/bbs.linuxtone.conf;  
  }
로그인 후 복사

2 upstream.conf 구성 파일(이는

  upstream.conf 
  upstream bbs.linuxtone.com { 
  server 192.168.1.4:8099; 
  }
로그인 후 복사

3 사이트 구성 로드를 위한 구성 방법이기도 함)

  bbs.linuxtone.conf 
  server 
  { 
  listen 80; 
  server_name bbs.linuxtone.conf; 
  charset gb2312; 
  index index.html index.htm; 
  root /date/wwwroot/linuxtone/; 
  location ~ ^/nginxstatus/ { 
  stub_status on; 
  access_log off; 
  } 
  location / { 
  root /date/wwwroot/linuxtone/; 
  proxy_redirect off ; 
  proxy_set_header host $host; 
  proxy_set_header x-real-ip $remote_addr; 
  proxy_set_header remote-host $remote_addr; 
  proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; 
  client_max_body_size 50m; 
  client_body_buffer_size 256k; 
  proxy_connect_timeout 30; 
  proxy_send_timeout 30; 
  proxy_read_timeout 60; 
  proxy_buffer_size 256k; 
  proxy_buffers 4 256k; 
  proxy_busy_buffers_size 256k; 
  proxy_temp_file_write_size 256k; 
  proxy_next_upstream error timeout invalid_header http_500 
  http_503 http_404; 
  proxy_max_temp_file_size 128m; 
  proxy_pass http://bbs.linuxtone.com; 
  }
로그인 후 복사

파일의 매개변수가 조정되었습니다. 목적은 프록시 프로세스 중에 발생하는 일부 502 499 오류를 해결하는 것입니다.

  #add expires header for static content 
  location ~* \.(jpg|jpeg|gif|png|swf)$ { 
  if (-f $request_filename) { 
  root /date/wwwroot/linuxtone/; 
  expires 1d; 
  break; 
  } 
  } 
  log_format access '$remote_addr - $remote_user [$time_local] "$request" ' 
  '$status $body_bytes_sent "$http_referer" ' 
  '"$http_user_agent" $http_x_forwarded_for'; 
  access_log /exp/nginxlogs/bbs.linuxtone_access.log access; 
  }
로그인 후 복사

공통 명령
nginx 리버스 프록시의 몇 가지 일반적인 명령을 살펴보겠습니다

proxy_pass 명령
Syntax

  proxy_pass  [url | upstream]
로그인 후 복사

Function
이 명령은 프록시 서버 포트나 소켓을 설정하는 데 사용되며, url

proxy_redirect 명령
Syntax

  proxy_redirect  [off | default | redirect replacement]
로그인 후 복사

Function
이 명령은 ""를 변경하는 데 사용됩니다. 프록시 서버 위치의 응답 헤더에" 및 "새로 고침"
추가:
아직 이 명령의 기능을 익히지 못했습니다. 실제 설정에서는 모두 꺼져 있습니다. 혹시 아시는 분은 남겨주세요 나를 안내하는 블로그 메시지

proxy_next_upstream 명령
Syntax

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

proxy_next_upstream [error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off]

기능
이 지시어는 요청이 다음 서버로 전달되는 상황을 설정하는 데 사용됩니다. 업스트림 로드 밸런싱 프록시 서버 풀에서 백엔드 서버 중 하나에 연결할 수 없거나 지정된 오류 응답 코드를 반환하는 경우 이를 사용할 수 있습니다.
매개변수 설명

error: 연결 시, 요청을 보낼 때, 응답 메시지를 읽을 때 오류가 발생하는 경우
timeout: 타임아웃이 발생하는 경우 서버에 연결할 때, 요청을 전송할 때 또는 백엔드 서버 응답 메시지를 읽을 때
valid_header: 백엔드 서버가 비어 있거나 잘못된 응답을 반환합니다.
http_[500|502|503|504|404]: 백엔드 서버가 지정된 응답 상태 코드를 반환합니다.
off: 다음 백엔드 서버로 요청을 전달하는 것을 금지합니다.

proxy_set_header command
Syntax

  proxy_set_header header value
로그인 후 복사

Function
This 지시문을 사용하면 프록시로 전달되는 요청 정보에 헤더 라인을 재정의하거나 추가할 수 있습니다. 값은 텍스트, 변수 또는 텍스트와 변수의 조합일 수 있습니다

위 내용은 Nginx 서버용 역방향 프록시를 구축하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:yisu.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!