To configure NGINX to handle WebSocket traffic ws:// or wss://, NGINX 에 웹소켓 설정하기

To configure NGINX to handle WebSocket traffic (ws:// or wss://), you need to set up NGINX to proxy WebSocket connections to your backend server. Here is a step-by-step guide on how to achieve this:

Step 1: Install NGINX

First, ensure that NGINX is installed on your server. You can install NGINX using your package manager.

For example, on Ubuntu, you can install NGINX with:

sudo apt update
sudo apt install nginx

Step 2: Configure NGINX for WebSocket

  1. Edit the NGINX Configuration File

    Open the NGINX configuration file. This is usually located at /etc/nginx/nginx.conf or in a specific site configuration file within /etc/nginx/sites-available/.

    sudo nano /etc/nginx/sites-available/default
    
  2. Add WebSocket Configuration

    Update the server block to include the necessary configuration to handle WebSocket traffic. Ensure that you have the proxy_set_header directives configured to support WebSocket connections.

    Here is an example configuration:

    server {
        listen 80;
    
        server_name example.com;
    
        location / {
            proxy_pass http://backend_server;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            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_set_header X-Forwarded-Proto $scheme;
        }
    }
    

    In this example:

    • example.com is your server's domain name.
    • backend_server should be replaced with the address of your backend WebSocket server. This could be another domain or an IP address with a port number, e.g., http://localhost:3000.
  3. Save and Close the File

    Save the changes and close the editor.

  4. Test the NGINX Configuration

    Before applying the new configuration, it's a good practice to test it to ensure there are no syntax errors:

    sudo nginx -t
    
  5. Reload NGINX

    If the configuration test is successful, reload NGINX to apply the changes:

    sudo systemctl reload nginx
    

Step 3: Configure SSL for Secure WebSocket (Optional)

If you want to use wss:// (secure WebSocket), you'll need to configure SSL in NGINX.

  1. Obtain an SSL Certificate

    You can obtain an SSL certificate from a Certificate Authority (CA) or use Let's Encrypt for a free SSL certificate.

  2. Update NGINX Configuration for SSL

    Modify your NGINX configuration to include SSL settings:

    server {
        listen 443 ssl;
        server_name example.com;
    
        ssl_certificate /path/to/your/cert.pem;
        ssl_certificate_key /path/to/your/cert.key;
    
        location / {
            proxy_pass http://backend_server;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            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_set_header X-Forwarded-Proto $scheme;
        }
    }
    
    server {
        listen 80;
        server_name example.com;
    
        location / {
            return 301 https://$host$request_uri;
        }
    }
    
    • Replace /path/to/your/cert.pem and /path/to/your/cert.key with the actual paths to your SSL certificate and key files.
    • The second server block redirects HTTP traffic to HTTPS.
  3. Test and Reload NGINX

    Test the configuration:

    sudo nginx -t
    

    If the test is successful, reload NGINX:

    sudo systemctl reload nginx
    

Conclusion

By following these steps, you can configure NGINX to handle WebSocket traffic for both ws:// and wss:// protocols. Ensure that your backend server is correctly handling WebSocket connections and that the proxy settings in NGINX are correctly forwarding the necessary headers for WebSocket communication.

댓글

이 블로그의 인기 게시물

PYTHONPATH, Python 모듈 환경설정

You can use Sublime Text from the command line by utilizing the subl command

git 명령어

[gRPC] server of Java and client of Typescript

[Ubuntu] Apache2.4.x 설치

Create topic on Kafka with partition count, 카프카 토픽 생성하기

리눅스의 부팅과정 (프로세스, 서비스 관리)

Auto-populate a calendar in an MUI (Material-UI) TextField component

The pierce selector in Puppeteer