search
HomeBackend DevelopmentPHP TutorialNginx reverse proxy websocket configuration example
Nginx reverse proxy websocket configuration exampleMay 08, 2018 pm 04:44 PM
nginxwebwebsocket

This article mainly introduces Nginx reverse proxy websocket configuration examples. This article is a summary of the project requirements after successful configuration. Friends in need can refer to it

There is a recent requirement that requires the use of nginx reverse Proxy websocket, after searching some information, it has been tested and passed. This article only makes a record

Copy code The code is as follows:

注: 看官方文档说 Nginx 在 1.3 以后的版本才支持 websocket 反向代理,所以要想使用支持 websocket 的功能,必须升级到 1.3 以后的版本,
因此我这边是下载的 Tengine 的最新版本测试的

1. Download tengine Recent source code

Copy code The code is as follows:

wget http://tengine.taobao.org/download/tengine-2.0.3.tar.gz

2. Install basic dependenciesPackage

Copy code The code is as follows:

yum -y install pcre*
yum -y install zlib*
yum -y install openssl*

3. Unzip, compile and install

Copy code The code is as follows :

tar -zxvf tengine-2.0.3.tar.gz cd tengine-2.0.3 ./configure --prefix=安装目录 make sudo make install

nginx.conf configuration is as follows:

Copy code The code is as follows:

user apps apps;
worker_processes  4; # 这个由于我是用的虚拟机,所以配置的 4 ,另外 tengine 可以自动根据CPU数目设置进程个数和绑定CPU亲缘性
# worker_processes auto
# worker_cpu_affinity auto
error_log  logs/error.log;
pid        logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events {
    use epoll;
    worker_connections  65535;
}
# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
#    load ngx_http_fastcgi_module.so;
#    load ngx_http_rewrite_module.so;
#}
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 4k;
    large_client_header_buffers 4 32k;
    client_max_body_size 80m;
    sendfile on;
    tcp_nopush     on;
    client_body_timeout  5;
    client_header_timeout 5;
    keepalive_timeout  5;
    send_timeout       5;
    open_file_cache max=65535 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;
    tcp_nodelay on;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    client_body_buffer_size  512k;
    proxy_connect_timeout    5;
    proxy_read_timeout       60;
    proxy_send_timeout       5;
    proxy_buffer_size        16k;
    proxy_buffers            4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    proxy_temp_path   /dev/shm/temp;
    proxy_cache_path  /dev/shm/cache levels=2:2:2   keys_zone=cache_go:200m inactive=5d max_size=7g;
    log_format log_access  '$remote_addr - $remote_user [$time_local] "$request" "$request_time" "$upstream_response_time"'
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for $host $hostname' ;
    #websocket 需要加下这个
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }
    include /home/apps/tengine/conf/test.com;
}

test. com configuration file content:

Copy code The code is as follows:

upstream test.com {
   server 192.168.1.5:9000;
}
server {
    listen       80;
    server_name  test.com;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    location  ^~  /websocket {
        proxy_pass http://test.com;
        proxy_redirect    off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

Parse the map command

nginx above For the function of map $http_upgrade $connection_upgrade in the .conf configuration, please refer to http://www.ttlsa.com/nginx/using-nginx-map-method/

This function is mainly based on the $ in the client request The value of http_upgrade is used to construct and change the value of $connection_upgrade, that is, a new variable $connection_upgrade is created based on the value of the variable $http_upgrade. The rules created are the contents of {}, please see configuration:

Copy Code The code is as follows:

map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

The rules are not matched, so the default is used, that is, the value of $connection_upgrade will always be upgrade. Then if $http_upgrade is an empty string, the value will be close. Personal understanding!

Related recommendations:

Nginx and PHP Installation and Configuration Six Nginx Reverse Proxy and Load Balancing Deployment Guide

nginx Reverse The proxy mechanism solves front-end cross-domain issues

The above is the detailed content of Nginx reverse proxy websocket configuration example. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
SpringBoot怎么整合WebSocket实现后端向前端发送消息SpringBoot怎么整合WebSocket实现后端向前端发送消息May 11, 2023 pm 02:07 PM

一、什么是websocket接口使用websocket建立长连接,服务端和客户端可以互相通信,服务端只要有数据更新,就可以主动推给客户端。WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据。在WebSocketAPI中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并进行双向数据传输。在WebSocketAPI中,浏览器和服务器只需要做一个握手的动作,然后,浏览器和服务器之间就形成了一条快速通道。两者之间就直接可以数据互相传送。

在ThinkPHP6中使用Nginx反向代理Websocket在ThinkPHP6中使用Nginx反向代理WebsocketJun 20, 2023 pm 09:31 PM

在近几年的互联网应用中,Websocket已经成为了一种非常重要的通信协议。ThinkPHP6作为一款优秀的PHP开发框架,也提供了对Websocket的支持。不过,在使用Websocket时,我们通常会涉及到跨域、负载均衡等问题,因此,在这篇文章中,我们将介绍如何在ThinkPHP6中使用Nginx反向代理Websocket。首先,我们需要明确一下Webs

Python服务器编程:实现WebSocket服务端Python服务器编程:实现WebSocket服务端Jun 19, 2023 am 09:51 AM

近年来,WebSocket技术日渐流行,成为了浏览器与服务器之间进行实时通信的标准选择。在Python中,我们可以通过一些成熟的库来实现WebSocket服务端的开发。本文将在介绍WebSocket技术的基础上,探索如何利用Python开发WebSocket服务端。一、什么是WebSocketWebSocket是一种在单个TCP

浏览器支持WebTransport?它能替代WebSockets?浏览器支持WebTransport?它能替代WebSockets?Feb 23, 2023 pm 03:36 PM

许多应用程序,如游戏和直播等场景,需要一种机制来尽可能快地发送消息,同时可以接受无序、不可靠的数据传输方式。本机应用程序虽然可以使用原始 UDP 套接字,但这些在 Web 上不可用,因为它们缺乏加密、拥塞控制、同意发送机制(以防止 DDoS 攻击)。

SpringBoot怎么实现WebSocket即时通讯SpringBoot怎么实现WebSocket即时通讯May 12, 2023 am 09:13 AM

1、引入依赖org.springframework.bootspring-boot-starter-websocketorg.projectlomboklombokcom.alibabafastjson1.2.32、WebSocketConfig开启WebSocketpackagecom.shucha.deveiface.web.config;/***@authortqf*@Description*@Version1.0*@since2022-04-1215:35*/importorg.spri

PHP+Socket系列之实现websocket聊天室PHP+Socket系列之实现websocket聊天室Feb 02, 2023 pm 04:39 PM

本篇文章给大家带来了关于php+socket的相关知识,其中主要介绍了怎么使用php原生socket实现一个简易的web聊天室?感兴趣的朋友下面一起来看一下,希望对大家有帮助。

Spring Boot中使用WebSocket实现推送和通知功能Spring Boot中使用WebSocket实现推送和通知功能Jun 23, 2023 am 11:47 AM

在现代Web应用程序开发中,WebSocket是实现即时通信和实时数据传输的常用技术。SpringBoot框架提供了集成WebSocket的支持,使得开发者可以非常方便地实现推送和通知功能。本文将介绍SpringBoot中如何使用WebSocket实现推送和通知功能,并演示一个简单的实时在线聊天室的实现。创建SpringBoot项目首先,我们需要创建一

如何在Linux中使用WebSocket技术如何在Linux中使用WebSocket技术Jun 18, 2023 pm 07:38 PM

随着现代网络应用程序的增多,WebSocket技术也变得非常流行。它是一项基于TCP协议的长连接技术,可以在客户端和服务器之间创建双向通信管道。在本文中,我们将介绍如何在Linux系统中使用WebSocket技术来创建一个简单的实时聊天应用程序。一、安装Node.js要使用WebSocket,首先需要在Linux系统中安装Node.j

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor