Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
nginx [2024/07/19 00:42] 127.0.0.1 Externe Bearbeitung |
nginx [2025/06/08 22:21] (aktuell) jango |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | NginX ist ein sehr leichtgewichtige aber extrem skalierbarer und robuster Webserver, [[proxy|Proxy]] und Load Balancer. | ||
+ | < | ||
+ | // No foreground console | ||
+ | nginx -g " | ||
+ | |||
+ | // test config | ||
+ | nginx -t | ||
+ | |||
+ | // reload config | ||
+ | nginx -s reload | ||
+ | |||
+ | // shutdown | ||
+ | nginx -s quit | ||
+ | |||
+ | // enable config | ||
+ | ln -s / | ||
+ | </ | ||
+ | |||
+ | =====HTTP===== | ||
+ | |||
+ | < | ||
+ | worker_processes | ||
+ | error_log | ||
+ | |||
+ | events { | ||
+ | worker_connections | ||
+ | } | ||
+ | |||
+ | http { | ||
+ | server { | ||
+ | listen | ||
+ | location / { | ||
+ | root html; | ||
+ | rewrite ^/test$ test.php permanent; | ||
+ | } | ||
+ | | ||
+ | # need to start php | ||
+ | # php-cgi.exe -b 127.0.0.1: | ||
+ | location ~ \.php$ { | ||
+ | fastcgi_pass 127.0.0.1: | ||
+ | fastcgi_index index.php; | ||
+ | fastcgi_param SCRIPT_FILENAME | ||
+ | include fastcgi_params; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | =====Proxy===== | ||
+ | |||
+ | < | ||
+ | server { | ||
+ | server_name subdomain1.example.com; | ||
+ | location / { | ||
+ | proxy_pass | ||
+ | } | ||
+ | } | ||
+ | server { | ||
+ | server_name subdomain2.example.com; | ||
+ | location / { | ||
+ | proxy_pass | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | =====SSL proxy===== | ||
+ | |||
+ | < | ||
+ | server { | ||
+ | listen 443 ssl; | ||
+ | server_name example.com; | ||
+ | |||
+ | ssl_certificate / | ||
+ | ssl_certificate_key / | ||
+ | |||
+ | # SSL-Protokolle und Cipher-Suiten konfigurieren (optional) | ||
+ | ssl_protocols TLSv1.2 TLSv1.3; | ||
+ | ssl_prefer_server_ciphers on; | ||
+ | ssl_ciphers ' | ||
+ | |||
+ | location / { | ||
+ | proxy_pass http:// | ||
+ | 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; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | =====Loadbalancer===== | ||
+ | |||
+ | Siehe [[https:// | ||
+ | |||
+ | Entweder in der Datei < | ||
+ | |||
+ | < | ||
+ | log_format upstreamlog ' | ||
+ | |||
+ | upstream myproject { | ||
+ | server 127.0.0.1: | ||
+ | server 127.0.0.1: | ||
+ | server 127.0.0.1: | ||
+ | server 127.0.0.1: | ||
+ | } | ||
+ | |||
+ | server { | ||
+ | listen 80; | ||
+ | server_name www.domain.com; | ||
+ | access_log / | ||
+ | location / { | ||
+ | proxy_pass http:// | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | =====RTMP===== | ||
+ | |||
+ | Für Windows [[https:// | ||
+ | |||
+ | < | ||
+ | worker_processes | ||
+ | |||
+ | error_log | ||
+ | |||
+ | events { | ||
+ | worker_connections | ||
+ | } | ||
+ | |||
+ | rtmp { | ||
+ | server { | ||
+ | listen 1935; | ||
+ | |||
+ | application live { | ||
+ | live on; | ||
+ | } | ||
+ | |||
+ | application hls { | ||
+ | live on; | ||
+ | hls on; | ||
+ | hls_path temp/ | ||
+ | hls_fragment 8s; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | http { | ||
+ | server { | ||
+ | listen | ||
+ | |||
+ | location / { | ||
+ | root html; | ||
+ | } | ||
+ | |||
+ | location /stat { | ||
+ | rtmp_stat all; | ||
+ | rtmp_stat_stylesheet stat.xsl; | ||
+ | } | ||
+ | |||
+ | location /stat.xsl { | ||
+ | root html; | ||
+ | } | ||
+ | |||
+ | location /hls { | ||
+ | #server hls fragments | ||
+ | types{ | ||
+ | application/ | ||
+ | video/mp2t ts; | ||
+ | } | ||
+ | alias temp/ | ||
+ | expires -1; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Um das RTMP Modul zu nutzen muss man nginx laut einigen Quellen selbst kompilieren | ||
+ | |||
+ | < | ||
+ | git clone https:// | ||
+ | apt-get install build-essential libpcre3 libpcre3-dev libssl-dev | ||
+ | wget http:// | ||
+ | tar -xf nginx-1.10.1.tar.gz | ||
+ | cd nginx-1.10.1 | ||
+ | ./configure --with-http_ssl_module --add-module=../ | ||
+ | make -j 1 | ||
+ | make install | ||
+ | </ | ||
+ | |||
+ | Anscheinend stimmt das aber nicht ganz ich habe mit apt die entsprechende lib installiert und es funktioniert genauso. | ||
+ | |||
+ | < | ||
+ | apt install libnginx-mod-rtmp | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | rtmp { | ||
+ | server { | ||
+ | listen 1935; | ||
+ | chunk_size 8192; | ||
+ | |||
+ | application stream { | ||
+ | live on; | ||
+ | meta copy; | ||
+ | push rtmp:// | ||
+ | on_play http:// | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | ffmpeg -re -i https:// | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | rtmp { | ||
+ | |||
+ | server { | ||
+ | |||
+ | listen 1935; | ||
+ | |||
+ | chunk_size 4000; | ||
+ | |||
+ | # TV mode: one publisher, many subscribers | ||
+ | application mytv { | ||
+ | |||
+ | # enable live streaming | ||
+ | live on; | ||
+ | |||
+ | # record first 1K of stream | ||
+ | record all; | ||
+ | record_path /tmp/av; | ||
+ | record_max_size 1K; | ||
+ | |||
+ | # append current timestamp to each flv | ||
+ | record_unique on; | ||
+ | |||
+ | # publish only from localhost | ||
+ | allow publish 127.0.0.1; | ||
+ | deny publish all; | ||
+ | |||
+ | #allow play all; | ||
+ | } | ||
+ | |||
+ | # Transcoding (ffmpeg needed) | ||
+ | application big { | ||
+ | live on; | ||
+ | |||
+ | # On every pusblished stream run this command (ffmpeg) | ||
+ | # with substitutions: | ||
+ | # | ||
+ | # This ffmpeg call receives stream from this application & | ||
+ | # reduces the resolution down to 32x32. The stream is the published to | ||
+ | # ' | ||
+ | # | ||
+ | # ffmpeg can do anything with the stream like video/audio | ||
+ | # transcoding, | ||
+ | # | ||
+ | # Multiple exec lines can be specified. | ||
+ | |||
+ | exec ffmpeg -re -i rtmp:// | ||
+ | -f flv rtmp:// | ||
+ | } | ||
+ | |||
+ | application small { | ||
+ | live on; | ||
+ | # Video with reduced resolution comes here from ffmpeg | ||
+ | } | ||
+ | |||
+ | application webcam { | ||
+ | live on; | ||
+ | |||
+ | # Stream from local webcam | ||
+ | exec_static ffmpeg -f video4linux2 -i /dev/video0 -c:v libx264 -an | ||
+ | -f flv rtmp:// | ||
+ | } | ||
+ | |||
+ | application mypush { | ||
+ | live on; | ||
+ | |||
+ | # Every stream published here | ||
+ | # is automatically pushed to | ||
+ | # these two machines | ||
+ | push rtmp1.example.com; | ||
+ | push rtmp2.example.com: | ||
+ | } | ||
+ | |||
+ | application mypull { | ||
+ | live on; | ||
+ | |||
+ | # Pull all streams from remote machine | ||
+ | # and play locally | ||
+ | pull rtmp:// | ||
+ | } | ||
+ | |||
+ | application mystaticpull { | ||
+ | live on; | ||
+ | |||
+ | # Static pull is started at nginx start | ||
+ | pull rtmp:// | ||
+ | } | ||
+ | |||
+ | # video on demand | ||
+ | application vod { | ||
+ | play /var/flvs; | ||
+ | } | ||
+ | |||
+ | application vod2 { | ||
+ | play /var/mp4s; | ||
+ | } | ||
+ | |||
+ | # Many publishers, many subscribers | ||
+ | # no checks, no recording | ||
+ | application videochat { | ||
+ | |||
+ | live on; | ||
+ | |||
+ | # The following notifications receive all | ||
+ | # the session variables as well as | ||
+ | # particular call arguments in HTTP POST | ||
+ | # request | ||
+ | |||
+ | # Make HTTP request & use HTTP retcode | ||
+ | # to decide whether to allow publishing | ||
+ | # from this connection or not | ||
+ | on_publish http:// | ||
+ | |||
+ | # Same with playing | ||
+ | on_play http:// | ||
+ | |||
+ | # Publish/ | ||
+ | on_done http:// | ||
+ | |||
+ | # All above mentioned notifications receive | ||
+ | # standard connect() arguments as well as | ||
+ | # play/ | ||
+ | # with GET-style syntax to play & publish | ||
+ | # these are also included. | ||
+ | # Example URL: | ||
+ | # | ||
+ | |||
+ | # record 10 video keyframes (no audio) every 2 minutes | ||
+ | record keyframes; | ||
+ | record_path /tmp/vc; | ||
+ | record_max_frames 10; | ||
+ | record_interval 2m; | ||
+ | |||
+ | # Async notify about an flv recorded | ||
+ | on_record_done http:// | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | # HLS | ||
+ | |||
+ | # For HLS to work please create a directory in tmpfs (/tmp/hls here) | ||
+ | # for the fragments. The directory contents is served via HTTP (see | ||
+ | # http{} section in config) | ||
+ | # | ||
+ | # Incoming stream must be in H264/AAC. For iPhones use baseline H264 | ||
+ | # profile (see ffmpeg example). | ||
+ | # This example creates RTMP stream from movie ready for HLS: | ||
+ | # | ||
+ | # ffmpeg -loglevel verbose -re -i movie.avi | ||
+ | # -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1 | ||
+ | # -f flv rtmp:// | ||
+ | # | ||
+ | # If you need to transcode live stream use ' | ||
+ | # | ||
+ | application hls { | ||
+ | live on; | ||
+ | hls on; | ||
+ | hls_path /tmp/hls; | ||
+ | } | ||
+ | |||
+ | # MPEG-DASH is similar to HLS | ||
+ | |||
+ | application dash { | ||
+ | live on; | ||
+ | dash on; | ||
+ | dash_path /tmp/dash; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | # HTTP can be used for accessing RTMP stats | ||
+ | http { | ||
+ | |||
+ | server { | ||
+ | |||
+ | listen | ||
+ | |||
+ | # This URL provides RTMP statistics in XML | ||
+ | location /stat { | ||
+ | rtmp_stat all; | ||
+ | |||
+ | # Use this stylesheet to view XML as web page | ||
+ | # in browser | ||
+ | rtmp_stat_stylesheet stat.xsl; | ||
+ | } | ||
+ | |||
+ | location /stat.xsl { | ||
+ | # XML stylesheet to view RTMP stats. | ||
+ | # Copy stat.xsl wherever you want | ||
+ | # and put the full directory path here | ||
+ | root / | ||
+ | } | ||
+ | |||
+ | location /hls { | ||
+ | # Serve HLS fragments | ||
+ | types { | ||
+ | application/ | ||
+ | video/mp2t ts; | ||
+ | } | ||
+ | root /tmp; | ||
+ | add_header Cache-Control no-cache; | ||
+ | } | ||
+ | |||
+ | location /dash { | ||
+ | # Serve DASH fragments | ||
+ | root /tmp; | ||
+ | add_header Cache-Control no-cache; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | =====Links===== | ||
+ | |||
+ | * [[https:// |