JAVA项目从运维部署到项目开发(五. Nginx)

时间:2022-05-23 07:11:50

Nginx与Nginx-rtmp-module搭建RTMP视频直播和点播服务器》一文简单介绍了关于直播数据流的nginx相关配置,下面简单介绍下各种项目如何配置nginx。

web项目、静态页面、websocket的配置

  web项目除了在Tomcat容器里做相应配置外,nginx的配置也很重要。以下为web项目、静态页面、websocket、rtmp项目的配置详情:

#user  nobody;
worker_processes ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections ;
}
#RTMP服务
rtmp {
server {
listen ; #//服务端口
chunk_size ; #//数据传输块的大小
application vod {
play /opt/video; #//视频文件存放位置。
}
#直播
application live{
live on;
record off;
#record all;
record_path /opt/video/hls;
#record_max_size 50M;
record_max_size 2M; hls on; #这个参数把直播服务器改造成实时回放服务器。
wait_key on; #对视频切片进行保护,这样就不会产生马赛克了
hls_path /opt/video/hls; #切片视频文件存放位置
hls_fragment 600s; #设置HLS片段长度
hls_playlist_length 1m; #设置HLS播放列表长度,这里设置的是10分钟
hls_continuous on; #连续模式
hls_cleanup on; #对多余的切片进行删除
hls_nested on; #嵌套模式
allow play all;
}
}
} 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 ;
keepalive_timeout ; #gzip on;
gzip on;
gzip_min_length 1k;
gzip_buffers 64k;
gzip_http_version 1.1;
gzip_comp_level ;
gzip_types text/plain application/x-javascript text/css application/xml application/json;
gzip_vary on; map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
} upstream wordoor_chat_api_clusters {
ip_hash;
server 192.168.1.31:;
server 192.168.1.31:;
} server {
listen ;
server_name api1.gopopon.com; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
add_header 'Access-Control-Allow-Origin' "*";
} #socket
location ~ ^/wordoor_chat_api/v1/websocket/(.*) {
proxy_pass http://wordoor_chat_api_clusters/chatrest/websocket/$?$args; proxy_read_timeout ;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
} #web项目
location /wordoor_chat_api/v1/ {
proxy_pass http://wordoor_chat_api_clusters/chatrest/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} #静态页面 "/coursestatistics"会自动拼接到"/apps/statics"后面去
location /coursestatistics{
root /apps/statics;
# index index.html index.htm; 不配置则默认 index.html
autoindex off;# 是否可以以目录形式打开
#add_header 'Access-Control-Allow-Origin' "*"; 跨域
} } server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location /wordoor_chat_api/v1/ {
proxy_pass http://wordoor_chat_api_clusters/chatrest/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
} include /usr/local/openresty/nginx/conf/server/*;
include /usr/local/openresty/nginx/conf/vhosts/*;
}

  配置完成后,检查nginx配置文件然后reload即可访问