- 配置安全组:我直接使用80端口监听ws流量,如需额外端口配置则增加安全组;
- 安装nginx:
yum -y install nginx
- 防火墙放行:
firewall-cmd --zone=public --add-port=80/tcp --permanent
- reload:
firewall-cmd --reload
- nginx配置(部分):
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
map $http_upgrade $connection_upgrade {
default keep-alive;
'websocket' upgrade;
}
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# 转发流量,如果没有ssl和域名也可以直接换成ip地址
location /ws {
proxy_pass https://websocket.example/ws_path;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
}
}
}
- 启动nginx