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
Edit the NGINX Configuration File
Open the NGINX configuration file. This is usually located at
/etc/nginx/nginx.confor in a specific site configuration file within/etc/nginx/sites-available/.sudo nano /etc/nginx/sites-available/defaultAdd WebSocket Configuration
Update the server block to include the necessary configuration to handle WebSocket traffic. Ensure that you have the
proxy_set_headerdirectives 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.comis your server's domain name.backend_servershould 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.
Save and Close the File
Save the changes and close the editor.
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 -tReload 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.
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.
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.pemand/path/to/your/cert.keywith the actual paths to your SSL certificate and key files. - The second server block redirects HTTP traffic to HTTPS.
- Replace
Test and Reload NGINX
Test the configuration:
sudo nginx -tIf 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.
댓글
댓글 쓰기