1.安装nginx
安装步骤:
# 安装依赖yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel# 解压缩tar -zxvf nginx-1.17.3.tar.gz# 进入解压目录cd nginx-1.17.3/# 执行配置./configure# 编译make# 安装(默认安装在/usr/local/nginx)make install
启动关闭命令:
# cd /usr/local/nginx启动命令:./sbin/nginx停止命令:./sbin/nginx -s stop/quit重启命令:./sbin/nginx -s reload
2.配置访问静态文件
#vi nginx.cong,首行添加user root;#server模块添加,假设需要访问服务器的/root/logs/rocketmqlogs文件夹下文件location ^~ /rocketmqlogs { root /root/logs/; autoindex on;}
浏览器访问:http://ip:8081/rocketmqlogs
3.配置反向代理
#server模块添加,假设需要转发的后端服务地址为http://IP:8123/api#浏览器访问方式http://nginx的ip:8081/api/,即可转发到http://IP:8123/api地址location ^~ /api { #代理的iip地址和端口号 proxy_pass http://IP:8123;}
4.转发websocket
http { #自定义变量 $connection_upgrade map $http_upgrade $connection_upgrade { default keep-alive; #默认为keep-alive 可以支持 一般http请求 'websocket' upgrade; #如果为websocket 则为 upgrade 可升级的。 } server { ... #假设web页面正常访问后端的websocket的服务访问地址为“ws:IP:8191/ws” #使用你nginx转发后的访问地址为“ws:nginx的IP:8081/ws” location ^~ /ws { proxy_pass http://IP:8191; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; #此处配置 上面定义的变量 proxy_set_header Connection $connection_upgrade; } }
页面访问地址:
5.websocket中断解决方法
#nginx配置http { server { location / { root html; index index.html index.htm; proxy_pass http://webscoket; proxy_http_version 1.1; proxy_connect_timeout 4s; #配置点1 proxy_read_timeout 60s; #配置点2,如果没效,可以考虑这个时间配置长一点 proxy_send_timeout 12s; #配置点3 proxy_set_header Upgrade $http_upgrade; #这是webSocket的配置,与此篇博客无关 proxy_set_header Connection "Upgrade"; #这是webSocket的配置,与此篇博客无关 } }}
web页面加入心跳检测:
https://www.jianshu.com/p/1141dcf6de3e
完整配置:
user root;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; #自定义变量 $connection_upgrade map $http_upgrade $connection_upgrade { default keep-alive; #默认为keep-alive 可以支持 一般http请求 'websocket' upgrade; #如果为websocket 则为 upgrade 可升级的。 } server { listen 8081; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #nginx默认配置 location / { root html; index index.html index.htm; } #配置静态文件访问 location ^~ /rocketmqlogs { root /root/logs/; autoindex on; } #配置反向代理 location ^~ /api { #代理的iip地址和端口号 proxy_pass http://61.153.48.134:8123; } #配置转发websocket location ^~ /ws { #代理的iip地址和端口号 proxy_pass http://61.153.48.134:8191; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; #此处配置 上面定义的变量 proxy_set_header Connection $connection_upgrade; } #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 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 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; # } #}}