[]의 Nginx Nginx에 배포 된 전투 - 정적 파일 + + 균형 리버스 프록시 부하 + + HTTPS 웹 소켓

Nginx는 러시아 디자이너 이고르 시솝 절차에 의한 고성능 웹 서버와 리버스 프록시, 또한 IMAP / POP3 / SMTP 프록시 서버를 개발한다. 나는 러시아의 이고르 시솝이 경찰에 의해 멀리 찍은 들었다 몇 년 전에, 나는 알고 넣어하지 않습니다. 정상으로 복귀를 말하는 방법의 nginx 구성에서 보면 우리의 일상 요구 사항을 충족합니다.

0 초콜릿의 nginx 설치

창문 바로 당신이 리눅스에서 설치하는 방법을 알고 싶다면, nginx를 설치 다른 기사 http://www.randyfield.cn/post/2019-06-01-netcore을 읽는 방법을 소개 있도록 블로거, 손 창에 있기 때문에 -centos / # 3의 nginx

나는 가상 코어가 아닌에서 기억 yum패키지 관리자, 그리고 그 아래에 그 창? 너무 쉽게 가지가 아니다? 대답은 '예, http://www.randyfield.cn/post/2019-06-01-netcore-centos/#4-%E5%AE%88%E6%8A%A4%E8%BF%9B%E7 A8 %.도 8b % ,

chocolatey

  • chocolatey 설치
Get-ExecutionPolicy

#如果返回的是Restricted
#就运行如下命令
Set-ExecutionPolicy AllSigned

#或者
Set-ExecutionPolicy Bypass -Scope Process

#接着执行-Paste the follow text into your shell and press Enter
#Wait a few seconds for the command to complete
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

#If you don't see any errors, you are ready to use Chocolatey
  • nginx를 설치
choco install nginx

참고로 두 지점이 있습니다

  1. 통해서 choco설치 nginx그것이 설치지지한다 NSSM좋은 것 인, 수 nginx로 등록되어 windows服务리눅스에서 동등 Supervisor하고PM2

  2. 으로 choco설치 nginx, 당신이 선택하는 마지막 필요성에 동의 y[Yes]하기 전에, 다시 쓰기의 내용에 관심을 지불 그가 실행하는 것입니다 C:\ProgramData\chocolatey\lib\nginx\tools\chocolateyInstall.ps1스크립트를

    $toolsDir = Split-Path -parent $MyInvocation.MyCommand.Definition
    . "$toolsDir\helpers.ps1"
    
    $pp = Get-PackageParameters
    
    $arguments = @{
        packageName = $env:chocolateyPackageName
        file        = "$toolsDir\nginx-1.17.8.zip"
        destination = if ($pp.installLocation) { $pp.installLocation } else { Get-ToolsLocation }
        port        = if ($pp.Port) { $pp.Port } else { 80 }
        serviceName = if ($pp.NoService) { $null } elseif ($pp.serviceName) { $pp.serviceName } else { 'nginx' }
    }
    
    if (-not (Assert-TcpPortIsOpen $arguments.port)) {
        throw 'Please specify a different port number...'
    }
    
    Install-Nginx $arguments
    

    메모 port예, 라인 80端口, nginx를 기본, 당신이 계속 그렇게하기 전에, 다음 검사가 80端口사용되지 않습니다, 그것은 설치에 의해 점령 될 것입니다 실패합니다. 어떻게 할까? 이것은 당신이 전용으로, 스크립트 위의 편집 할 필요가있다 80을 수정합니다 未被占用的端口. 그런 다음 명령 창을 다시 진행, Y를 선택, 당신은 성공적이 될 수 있습니다.

    예를 들어, 내가로 변경

    $toolsDir = Split-Path -parent $MyInvocation.MyCommand.Definition
    . "$toolsDir\helpers.ps1"
    
    $pp = Get-PackageParameters
    
    $arguments = @{
        packageName = $env:chocolateyPackageName
        file        = "$toolsDir\nginx-1.17.8.zip"
        destination = if ($pp.installLocation) { $pp.installLocation } else { Get-ToolsLocation }
        port        = if ($pp.Port) { $pp.Port } else { 81 }
        serviceName = if ($pp.NoService) { $null } elseif ($pp.serviceName) { $pp.serviceName } else { 'nginx' }
    }
    
    if (-not (Assert-TcpPortIsOpen $arguments.port)) {
        throw 'Please specify a different port number...'
    }
    
    Install-Nginx $arguments

설치 nginxC:\tools아래 구성 파일은 다음 섹션 초기화 기본값으로 구성됩니다

1. 초기 구성

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    #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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       81;
    #    listen       81;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       81 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

2. 배포 정적 파일

사실, 그 정적 초기화 파일 배포입니다

/받는 사람 요청 html폴더 index.html파일

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       81;
        server_name  localhost;

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

3. 배포 정적 파일 뷰 내장

뷰는 공식 문서를 인용 - " vue-router기본 hash 模式- URL을 변경, 페이지가 다시로드하지 않도록하면, URL의 전체 해시를 시뮬레이션하기 위해 URL을 사용하지만, 여기 url이되고 #, 그것은 단지 변경 될 수 있습니다 역사 모드 하지만,이 모드는 잘 작동하지만, 또한 배경 구성을 지원합니다. 우리의 응용 프로그램은 단일 페이지 클라이언트 응용 프로그램이기 때문에,에 사용자가 직접 브라우저가 잘 보이지 않는 404을 반환합니다 액세스 할 때 배경이 올바른 구성하지 않은 경우. 당신이 필요 플러스 try_files $uri $uri/ /index.html;"

try_files되는 의미 : 첫째가 일치하지 않습니다 $uri文件일치하지 않을 경우, $url/文件이 경우, 갈 것이다/index.html

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       81;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
            #proxy_redirect off;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

4. 역방향 프록시

단리 도메인 앞에 전방의 후단과 후단의 배포

백엔드 조각 등 IIS, 노드, 아파치 웹 서버에 배포 할 수 있지만, 우리는 하드 코드에 백 엔드 API 요청 URL의 프런트 엔드를하지 않으며, 백엔드 간 추가 요청 헤더를 허용하지 않는 경우, 브라우저는 차단됩니다.

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       81;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
            #proxy_redirect off;
        }
        
        location /api/ {
            proxy_pass http://ip:port/outside/api/; #反向代理
            proxy_http_version 1.1;
            
            proxy_set_header Host $http_host;
            proxy_cookie_path /api /;
            
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

그 호출 인터페이스 엔드 http://ip:81/api/요청, 리버스 프록시에있을 것이다 http://ip:port/outside/api/당신이 크로스 도메인 구성을 허용하는 배경이없는 경우, 다음이 솔루션 브라우저를 속이는 방법입니다.

5.로드 밸런싱

병렬 효율 이점 일련 이외에 인기 전방 및 분리 모드 후단 디커플링, 신속하게 문제를 찾아 수평 확대가 문제 전에 개발 생산성의 해방 종료 후 해결한다. 동시에 병행 큰 경우, 전방 및 후방 - 엔드 서버는 수평 부하 분산의 이용에 확장 프로그램을 확장 할 수있다.

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    #api接口部署至3个不同的地方
    upstream centerapi{
        server 192.168.110.10:82 weight=5;
        server 192.168.110.11:82 weight=3;
        server 192.168.110.10:83 weight=2;
    }

    server {
        listen       81;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
            #proxy_redirect off;
        }
        
        location /api/ {
            proxy_pass http://centerapi/; #反向代理
            proxy_http_version 1.1;
            proxy_redirect off;
            
            proxy_set_header Host $http_host;
            proxy_cookie_path /api /;
            
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
            
            #暴露反向代理的地址
            #add_header backendIP $upstream_addr;
            #add_header backendCode $upstream_status;      
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

요청 여기에 api接口, nginx그것은 다음과 같은 세 가지 권한 요청 URL에 따라 할당 다시합니다

 upstream centerapi{
        server 192.168.110.10:82 weight=5;
        server 192.168.110.11:82 weight=3;
        server 192.168.110.10:83 weight=2;
    }

할 때마다가 아닌 다른 주소를 테스트 할 경우, 모든 요청을 해결하기 위해 앞으로의 nginx 수 있도록 다음과 같은 구성과 함께 브라우저에 반환된다

 #暴露反向代理的地址
 add_header backendIP $upstream_addr;
 add_header backendCode $upstream_status;  

6. 서브 시스템 통합 구축

저희 회사는이 ASP.NET MVC오래된 프로젝트의 융합이 새로운 프로젝트의 전면과 후면 끝에서 분리

새로운 프로젝트의 중심 중 하나가 여러 센터를 포함하여 오래된 프로젝트 플랫폼,

  • /--- 역방향 프록시하는 IIS배포 ASP.NET MVC마스터 국
  • ^~/center1--- 프런트 엔드 빌드 정적 페이지의 nginx에 의해 게시되었습니다
  • ^~/center1/api/--- 사용 API 프런트 엔드 인터페이스, 리버스 프록시로드 밸런싱 +
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    upstream centerapi{
        server 192.168.110.10:82 weight=5;
        server 192.168.110.11:82 weight=3;
        server 192.168.110.10:83 weight=2;
    }
    server {
        listen       80;
        server_name  192.168.110.10;

        #主站
        location / {
            proxy_pass http://192.168.110.10/;#ASP.NET MVC主站 IIS已部署
            proxy_http_version 1.1;
            proxy_redirect off;
                 
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
        }
        
        #新中心前端
        location ^~/center1 {
            proxy_pass http://192.168.110.10:81/;#前端构建的静态页面 即下面的配置
            proxy_http_version 1.1;
            proxy_redirect off;
            
            proxy_set_header Host $host;         
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
        }
        
        #新中心api
        location ^~/center1/api/ {
            proxy_pass http://centerapi/;#前端调用的接口
            proxy_http_version 1.1;
            proxy_redirect off;
            
            proxy_set_header Host $host;         
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
        }
           
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       81;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
            #proxy_redirect off;
        }
        
        #location /api/ {
            #proxy_pass http://centerapi/; #反向代理
            #proxy_http_version 1.1;
            
            #proxy_set_header Host $http_host;
            #proxy_cookie_path /api /;
            
            #proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection keep-alive;
            #proxy_cache_bypass $http_upgrade;
            
            #暴露反向代理的地址
            #add_header backendIP $upstream_addr;
            #add_header backendCode $upstream_status;      
        #}
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

7.Https

당신이 HTTPS를 통해 사이트에 액세스 할 수 있도록하기 전에 IIS를 리더십, 새로운 프로젝트가 필요, https필요 ssl证书, 그렇지 않으면보고되지 않습니다, 사람들은 시만텍, 러스트, 지오 트러스트 (GeoTrust)와 같은 브라우저를 인식, 만 대기업의 SSL 인증서 인증서를 알고 신뢰할 수있는 인증서. 그러나 리더십의 요구를 충족하기 위해, 또는하여 openssl관련 인증서를 생성.

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    upstream centerapi{
        server 192.168.110.10:82 weight=5;
        server 192.168.110.11:82 weight=3;
        server 192.168.110.10:83 weight=2;
    }
    server {
        listen       80;
        server_name  192.168.110.10;
        
        #ssl开启
        ssl on;
        ssl_certificate ssl/server.crt;
        ssl_certificate_key ssl/server.key;

        location / {
            proxy_pass http://192.168.110.10/;#ASP.NET MVC主站
            proxy_http_version 1.1;
            proxy_redirect off;
                 
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
        }
        
        location ^~/center1 {
            proxy_pass http://centerapi/;#前端构建的静态页面 即下面的配置
            proxy_http_version 1.1;
            proxy_redirect off;
            
            proxy_set_header Host $host;         
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
        }
        
        location ^~/center1/api/ {
            proxy_pass http://192.168.110.10:81/;#前端调用的接口
            proxy_http_version 1.1;
            proxy_redirect off;
            
            proxy_set_header Host $host;         
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
        }
        
       
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       81;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
            #proxy_redirect off;
        }
        
        #location /api/ {
            #proxy_pass http://centerapi/; #反向代理
            #proxy_http_version 1.1;
            
            #proxy_set_header Host $http_host;
            #proxy_cookie_path /api /;
            
            #proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection keep-alive;
            #proxy_cache_bypass $http_upgrade;
            
            #暴露反向代理的地址
            #add_header backendIP $upstream_addr;
            #add_header backendCode $upstream_status;      
        #}
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

8.Websocket

시스템이 갑자기 증가 websocket的服务에 직접 액세스 ws://ip:port프런트 엔드는 하드 코딩, 그래서에 종지부를 찍을해야 nginx다시 재생할 수 있습니다.

websocket协议달리 http协议하지만 websocket의한 핸드 셰이크 http통신 프로토콜에 의해 구현은, HTTP에서 조향 웹 소켓을 강화한다.

8.1 WS

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    upstream centerapi{
        server 192.168.110.10:82 weight=5;
        server 192.168.110.11:82 weight=3;
        server 192.168.110.10:83 weight=2;
    }
    upstream websocket{
        server 192.168.110.10:1443 weight=1;
    }
    server {
        listen       80;
        server_name  192.168.110.10;
        
        #ssl    on;
        #ssl_certificate ssl/server.crt;
        #ssl_certificate_key ssl/server.key;

        location / {
            proxy_pass http://192.168.110.10/;#ASP.NET MVC主站
            proxy_http_version 1.1;
            proxy_redirect off;
                 
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
        }
        
        location ^~/center1 {
            proxy_pass http://centerapi/;#前端构建的静态页面 即下面的配置
            proxy_http_version 1.1;
            proxy_redirect off;
            
            proxy_set_header Host $host;         
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
        }
        
        location ^~/center1/api/ {
            proxy_pass http://192.168.110.10:81/;#前端调用的接口
            proxy_http_version 1.1;
            proxy_redirect off;
            
            proxy_set_header Host $host;         
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
        }
        
        #websocket
        location ^~/websocket/ {
            proxy_pass http://websocket;#前端调用的接口
            proxy_http_version 1.1;
            proxy_redirect off;
            
            proxy_set_header Host $host;         
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_connect_timeout 4s;
            proxy_read_timeout 600;
            proxy_send_timeout 12s;
        }
       
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       81;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
            #proxy_redirect off;
        }
        
        #location /api/ {
            #proxy_pass http://centerapi/; #反向代理
            #proxy_http_version 1.1;
            
            #proxy_set_header Host $http_host;
            #proxy_cookie_path /api /;
            
            #proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection keep-alive;
            #proxy_cache_bypass $http_upgrade;
            
            #暴露反向代理的地址
            #add_header backendIP $upstream_addr;
            #add_header backendCode $upstream_status;      
        #}
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

8.2 WSS

(HTTP 액세스 웹 사이트, 인터페이스) 정상적인 상황에서, 위의 구성이 완료이지만, 한 번 ssl on, 원래는 ws, 그것은 콘솔 출력으로 이어질 것입니다

Mixed Content: The page at ‘https://{域名}.com/‘ was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint ‘ws://{ip}:{port}/‘. This request has been blocked; this endpoint must be available over WSS.
Uncaught DOMException: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS.

당신은 다음과 같은 구성이 필요합니다

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    upstream centerapi{
        server 192.168.110.10:82 weight=5;
        server 192.168.110.11:82 weight=3;
        server 192.168.110.10:83 weight=2;
    }
    upstream websocket{
        server 192.168.110.10:1443 weight=1;
    }
    server {
        listen       80;
        server_name  192.168.110.10;
        
        ssl on;
        ssl_certificate ssl/server.crt;
        ssl_certificate_key ssl/server.key;
        ssl_session_timeout  5m;
        ssl_session_cache shared:SSL:50m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2  SSLv2 SSLv3;
        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers   on;

        location / {
            proxy_pass http://192.168.110.10/;#ASP.NET MVC主站
            proxy_http_version 1.1;
            proxy_redirect off;
                 
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
        }
        
        location ^~/center1 {
            proxy_pass http://centerapi/;#前端构建的静态页面 即下面的配置
            proxy_http_version 1.1;
            proxy_redirect off;
            
            proxy_set_header Host $host;         
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
        }
        
        location ^~/center1/api/ {
            proxy_pass http://192.168.110.10:81/;#前端调用的接口
            proxy_http_version 1.1;
            proxy_redirect off;
            
            proxy_set_header Host $host;         
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_cache_bypass $http_upgrade;
        }
        
        location ^~/websocket/ {
            proxy_pass http://websocket;#前端调用的接口
            proxy_http_version 1.1;
            proxy_redirect off;
            
            proxy_set_header Host $host;         
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_connect_timeout 4s;
            proxy_read_timeout 600;
            proxy_send_timeout 12s;
        }
       
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       81;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
            #proxy_redirect off;
        }
        
        #location /api/ {
            #proxy_pass http://centerapi/; #反向代理
            #proxy_http_version 1.1;
            
            #proxy_set_header Host $http_host;
            #proxy_cookie_path /api /;
            
            #proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection keep-alive;
            #proxy_cache_bypass $http_upgrade;
            
            #暴露反向代理的地址
            #add_header backendIP $upstream_addr;
            #add_header backendCode $upstream_status;      
        #}
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

9. 자주 묻는 질문 (FAQ)

Nginx에 앞으로 헤더 정보가 손실되는 경우

Nginx의를 통해 전달 후, 헤더 정보가 access_token은 손실됩니다, 조사 후, 밑줄 _ 속성 헤더없이 전달 기본적으로, 우리는 할당을 늘려야합니다, Nginx에 전달할 발견

server {
        listen       80;
        server_name  127.0.0.1;
        
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        underscores_in_headers on;
}

리버스 프록시 작업 시간이 초과되었습니다

역방향 프록시 인터페이스는, 인터페이스 처리 사업에 60 초 이상, 그것을보고 time out오류를.

 proxy_read_timeout 600;

10. 참조 링크

https://blog.csdn.net/qq_29663071/article/details/80759098

https://www.nginx.com/blog/websocket-nginx/

https://blog.csdn.net/duyiwuerluozhixiang/article/details/100358930

추천

출처www.cnblogs.com/RandyField/p/12535977.html