nginx配置-最后整理版

本文详细介绍Nginx的配置方法,包括错误日志记录、JSON格式日志、代理配置、状态监控模块设置、TCP端口映射等高级功能,并演示如何使用htpasswd进行基本认证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nginx配置
  • 错误日志记录
  • 日志json格式
  • stub_status & 开启认证
  • 404错误页配置,并重定向
  • 某些后缀文件拒绝访问(default.conf)
  • 配置include(简化)
worker_processes auto;
worker_rlimit_nofile 65535;
error_log stderr notice;
error_log /var/log/nginx/error.log;

events {
    multi_accept on;
    use epoll;
    worker_connections 51200;
}

http {
    include                       mime.types;
    default_type                  application/octet-stream;
    server_name_in_redirect       off;
    client_max_body_size          20m;
    client_header_buffer_size     16k;
    large_client_header_buffers 4 16k;
    sendfile                      on;
    tcp_nopush                    on;
    keepalive_timeout             65;
    server_tokens                 off;
    gzip                          on;
    gzip_min_length               1k;
    gzip_buffers                  4 16k;
    gzip_proxied                  any;
    gzip_http_version             1.1;
    gzip_comp_level               3;
    gzip_types                    text/plain application/x-javascript text/css application/xml;
    gzip_vary                     on;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    log_format json '{"@timestamp": "$time_iso8601",'
    '"@version": "1",'
    '"client": "$remote_addr",'
    '"url": "$uri", '
    '"status": $status, '
    '"domain": "$host", '
    '"host": "$server_addr",'
    '"size":"$body_bytes_sent", '
    '"response_time": $request_time, '
    '"referer": "$http_referer", '
    '"http_x_forwarded_for": "$http_x_forwarded_for", '
    '"ua": "$http_user_agent" } ';

    #access_log  /var/log/nginx/access.log  json;
    upstream owncloud {
        server 127.0.0.1:8000;
    }
    server {
        listen       80;
        server_name  ownclouds.maotai.org;
        location / {
            proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;
            proxy_pass http://owncloud;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

    upstream gogs {
        server 127.0.0.1:53000;
    }
    server {
        listen       80;
        server_name  gogs.maotai.org;
        location / {
            proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;
            proxy_pass http://gogs;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    server {
        listen 80;
        server_name 192.168.100.60;
        location /ngx_status {
            stub_status on;
            access_log off;
            allow all;
        }
    }
}
nginx精简版配置-包含
worker_processes auto;
worker_rlimit_nofile 65535;
error_log stderr notice;
error_log /var/log/nginx/error.log;

events {
    multi_accept on;
    use epoll;
    worker_connections 51200;
}

http {
    include                       mime.types;
    default_type                  application/octet-stream;
    server_name_in_redirect       off;
    client_max_body_size          20m;
    client_header_buffer_size     16k;
    large_client_header_buffers 4 16k;
    sendfile                      on;
    tcp_nopush                    on;
    keepalive_timeout             65;
    server_tokens                 off;
    gzip                          on;
    gzip_min_length               1k;
    gzip_buffers                  4 16k;
    gzip_proxied                  any;
    gzip_http_version             1.1;
    gzip_comp_level               3;
    gzip_types                    text/plain application/x-javascript text/css application/xml;
    gzip_vary                     on;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    log_format json '{"@timestamp": "$time_iso8601",'
    '"@version": "1",'
    '"client": "$remote_addr",'
    '"url": "$uri", '
    '"status": $status, '
    '"domain": "$host", '
    '"host": "$server_addr",'
    '"size":"$body_bytes_sent", '
    '"response_time": $request_time, '
    '"referer": "$http_referer", '
    '"http_x_forwarded_for": "$http_x_forwarded_for", '
    '"ua": "$http_user_agent" } ';

    include /etc/nginx/conf.d/*.conf;
}
  • /etc/nginx/conf.d/www.maotai.com

    server {
      listen       80;
      server_name  localhost;
      access_log  /var/log/nginx/host.access.log  main;
      location / {
          root   /usr/share/nginx/html;
          index  index.html index.htm;
      }
    }
  • /etc/nginx/conf.d/default.conf(某些后缀拒绝访问)

    server {
      listen       80;
      server_name  localhost;
    
      #charset koi8-r;
      #access_log  /var/log/nginx/host.access.log  main;
    
      location / {
          root   /usr/share/nginx/html;
          index  index.html index.htm;
      }
    
      #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   /usr/share/nginx/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;
      #}
    }
  • /etc/nginx/conf.d/nginx-status.conf

    
    #==================================================
    
    
    #                nginx status start
    
    
    #==================================================
    
      #yum install -y httpd-tools
      ## 首次创建密码文件
      #htpasswd -cmb /usr/local/nginx/conf/.pass admin 123456
      #
    
    ## 添加用户
    
      #htpasswd .pass guest
      #
    
    ## 改密码
    
      #htpasswd .pass fdipzone
    
      ## 删除用户
      #htpasswd -D .pass guest
      server {
          listen 80;
          server_name 192.168.100.60;
          auth_basic "secret";
          auth_basic_user_file /etc/nginx/conf/.pass;
    
          location /ngx_status {
              stub_status on;
              allow all;
              access_log off;
              allow 127.0.0.1;
              allow 192.168.1.0/24;
              allow 192.168.100.0/24;
              deny all;
          }
    
          #第三方状态模块: https://github.com/vozlt/nginx-module-vts
          location /ngx_statuss {
              vhost_traffic_status_display;
              vhost_traffic_status_display_format html;
              access_log off;
              allow 127.0.0.1;
              allow 192.168.1.0/24;
              allow 192.168.100.0/24;
              deny all;
          }
      }
nginx配置详细版-无include
worker_processes auto;
worker_rlimit_nofile 65535;
error_log stderr notice;
error_log /var/log/nginx/error.log;

events {
    multi_accept on;
    use epoll;
    worker_connections 51200;
}

http {
    include                       mime.types;
    default_type                  application/octet-stream;
    server_name_in_redirect       off;
    client_max_body_size          20m;
    client_header_buffer_size     16k;
    large_client_header_buffers 4 16k;
    sendfile                      on;
    tcp_nopush                    on;
    keepalive_timeout             65;
    server_tokens                 off;
    gzip                          on;
    gzip_min_length               1k;
    gzip_buffers                  4 16k;
    gzip_proxied                  any;
    gzip_http_version             1.1;
    gzip_comp_level               3;
    gzip_types                    text/plain application/x-javascript text/css application/xml;
    gzip_vary                     on;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    log_format json '{"@timestamp": "$time_iso8601",'
    '"@version": "1",'
    '"client": "$remote_addr",'
    '"url": "$uri", '
    '"status": $status, '
    '"domain": "$host", '
    '"host": "$server_addr",'
    '"size":"$body_bytes_sent", '
    '"response_time": $request_time, '
    '"referer": "$http_referer", '
    '"http_x_forwarded_for": "$http_x_forwarded_for", '
    '"ua": "$http_user_agent" } ';

    #access_log  /var/log/nginx/access.log  json;
    upstream owncloud {
        server 127.0.0.1:8000;
    }
    server {
        listen       80;
        server_name  ownclouds.maotai.org;
        location / {
            proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;
            proxy_pass http://owncloud;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }


    upstream gogs {
        server 127.0.0.1:53000;
    }
    server {
        listen       80;
        server_name  gogs.maotai.org;
        location / {
            proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;
            proxy_pass http://gogs;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    #==================================================
    #                nginx status start
    #==================================================
    #yum install -y httpd-tools
    ## 首次创建密码文件
    #htpasswd -cmb /usr/local/nginx/conf/.pass admin 123456
    #
## 添加用户
    #htpasswd .pass guest
    #
## 改密码
    #htpasswd .pass fdipzone

    ## 删除用户
    #htpasswd -D .pass guest
    server {
        listen 80;
        server_name 192.168.100.60;
        #        auth_basic "secret";
        #        auth_basic_user_file /etc/nginx/conf/.pass;

        location /ngx_status {
            stub_status on;
            allow all;
            access_log off;
            #            allow 127.0.0.1;
            #            allow 192.168.1.0/24;
            #            allow 192.168.100.0/24;
            #            deny all;
        }

        #--------- https://github.com/vozlt/nginx-module-vts
        #        location /ngx_statuss {
        #            vhost_traffic_status_display;
        #            vhost_traffic_status_display_format html;
        #            access_log off;
        #            allow 127.0.0.1;
        #            allow 192.168.1.0/24;
        #            allow 192.168.100.0/24;
        #            deny all;
        #        }

    }
    #==================================================
    #                nginx status stop
    #==================================================
}
nginx tcp端口映射
error_log stderr notice;

worker_processes auto;
events {
  multi_accept on;
  use epoll;
  worker_connections 1024;
}

stream {
        upstream kube_apiserver {
            least_conn;
            server 192.168.8.161:6443;
            server 192.168.8.162:6443;
            server 192.168.8.163:6443;
                    }

        server {
            listen        127.0.0.1:6443;
            proxy_pass    kube_apiserver;
            proxy_timeout 10m;
            proxy_connect_timeout 1s;

        }
}

nginx列出目录和认证

 error_log /usr/local/nginx/logs/error.log;

    server {
        listen 80;
        server_name 192.168.60.123;

        location / {
            root /server/soft_pkgs;
            autoindex on;
            autoindex_localtime on; #之类的参数写这里
            autoindex_exact_size off;
        }
        location /ngx_status {
            auth_basic "secret";
            auth_basic_user_file /usr/local/nginx/conf/.pass;
            stub_status on;
            # access_log  off;
            allow 127.0.0.1;
            allow 192.168.10.0/24;
            allow 192.168.60.0/24;
            deny all;
        }
        location /ngx_statuss {
            auth_basic "secret";
            auth_basic_user_file /usr/local/nginx/conf/.pass;
            check_status;
            # access_log off;
            #allow IP;
            #deny all;
        }
    }

htpasswd使用

# yum install -y httpd-tools
## 首次创建密码文件
    #htpasswd -cmb /usr/local/nginx/conf/.pass admin 123456
    #
## 添加用户
    #htpasswd .pass guest
    #
## 改密码
    #htpasswd /usr/local/nginx/conf/.pass  admin

## 删除用户
    #htpasswd -D .pass guest

nginx_upstream_check_module


参考:
http://nolinux.blog.51cto.com/4824967/1594029

    upstream nexus {
        server 192.168.66.222:8081;
        check interval=60000 rise=2 fall=5 timeout=1000 type=tcp; #仅需要添加这行即可
    }


    server {
        listen 80;
        server_name 192.168.60.123;
        auth_basic "secret";
        auth_basic_user_file /usr/local/nginx/conf/.pass;
        location /ngx_status {
            stub_status on;
            # access_log  off;
            allow 127.0.0.1;
            allow 192.168.10.0/24;
            allow 192.168.60.0/24;
            deny all;
        }
        location /ngx_statuss {
            check_status;
            # access_log off;
            #allow IP;
            #deny all;
        }
    }

nginx-module-vts


参考:
https://github.com/vozlt/nginx-module-vts#installation

https://github.com/kubernetes/ingress-nginx

mkdir /usr/local/nginx/3rdmodules
cd /usr/local/nginx/3rdmodules
git clone git://github.com/vozlt/nginx-module-vts.git

./configure --user=nginx --group=nginx --prefix=/usr/local/tengine-2.1.2 --with-http_stub_status_module --with-http_ssl_module --add-module=/usr/local/nginx/3rdmodules/nginx-module-vts
make && make install

    server {
        listen 80;
        server_name 192.168.6.123;
        auth_basic "secret";
        auth_basic_user_file /usr/local/nginx/conf/.pass;

        location /ngx_status {
            stub_status on;
            allow 127.0.0.1;
            allow 192.168.8.0/24;
            allow 192.168.9.0/24;
            deny all;
        }
        location /ngx_statuss {
            stub_status on;
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
            allow 127.0.0.1;
            allow 192.168.8.0/24;
            allow 192.168.9.0/24;
            deny all;
        }
    }
nginx补丁

http://jweiang.blog.51cto.com/8059417/1433675

vhost_traffic_status第三方nginx状态模块

nginx默认配置
[root@node1 ~]# docker run -it --rm nginx bash
root@ecbb058e6434:/# cat /etc/nginx/nginx.conf 

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
root@ecbb058e6434:/# cat /etc/nginx/conf.d/default.conf 
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #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   /usr/share/nginx/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;
    #}
}

<think>嗯,用户现在想在阿里云的Ubuntu 24.04系统上安装Nginx,并且还要配置nginx-rtmp-module v1.2.2和nginx-http-flv-module模块。之前我已经给过一个安装指南,但用户现在可能遇到了问题,需要更详细的步骤或者解决某些错误。 首先,我需要回顾用户提供的引用内容。引用[1]提到了检查配置文件时的参数,包括添加rtmp模块,但用户现在需要的是特定本v1.2.2。引用[2]显示启动nginx失败,可能是因为服务未正确安装。引用[3]提到了编译错误,需要修改源代码,这可能是因为模块本与Nginx不兼容。引用[4]是安装php包的问题,可能和当前问题无关,但需要注意依赖项是否满足。 用户明确提到要安装特定本的nginx-rtmp-module v1.2.2,而之前的回答可能用的是最新本。因此,需要确认如何获取该特定本。可能需要通过git checkout来切换到对应的tag或commit。 另外,用户之前的问题中提到的步骤可能缺少一些关键点,比如在编译安装后如何注册systemd服务,因为引用[2]中启动失败,提示服务未找到。因此,安装完成后可能需要手动创建nginx.service文件,并启用服务。 此外,用户可能在编译过程中遇到错误,如引用[3]中的模块代码错误,可能需要修改源码。需要确保使用的模块本与Nginx本兼容。例如,nginx-rtmp-module的v1.2.2可能需要特定本的Nginx,否则会有结构体成员不存在的错误,需要注释掉相关代码行。 接下来,我需要整理步骤,确保用户能正确下载指定本的模块,配置正确的参数,处理可能出现的编译错误,以及正确设置systemd服务。同时,需要提醒用户安装依赖项,如PCRE、OpenSSL、zlib等,避免出现依赖问题。 可能还需要测试配置文件的正确性,使用nginx -t命令,并在配置文件中添加RTMP和HTTP-FLV的配置块。用户可能需要指导如何编写这些配置,例如rtmp部分的设置,以及如何与HTTP模块结合使用。 最后,生成相关问题时要考虑用户可能的后续需求,比如推流测试、防火墙设置、多实例配置等,帮助用户进一步应用安装好的服务。</think>### 分步安装指南(Ubuntu 24.04 + Nginx + RTMP v1.2.2 + HTTP-FLV) --- #### **一、解决依赖问题** ```bash sudo apt update && sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev git ``` 必须安装编译工具链和基础库(PCRE、Zlib、OpenSSL)[^1]。 --- #### **二、获取指定本源码** 1. **下载Nginx源码**(选择兼容本,推荐1.25.x): ```bash cd /usr/src wget http://nginx.org/download/nginx-1.25.3.tar.gz tar zxvf nginx-1.25.3.tar.gz ``` 2. **获取指定本的模块**: ```bash # RTMP模块v1.2.2(需通过Git切换本) git clone https://github.com/arut/nginx-rtmp-module.git cd nginx-rtmp-module && git checkout v1.2.2 && cd .. # HTTP-FLV模块(使用最新稳定) git clone https://github.com/winshining/nginx-http-flv-module.git ``` --- #### **三、配置编译参数** ```bash cd nginx-1.25.3/ ./configure \ --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --modules-path=/usr/lib/nginx/modules \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/run/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=www-data \ --group=www-data \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_realip_module \ --with-threads \ --with-file-aio \ --add-module=../nginx-rtmp-module \ --add-module=../nginx-http-flv-module ``` 关键配置说明: - `--add-module` 确保模块路径正确[^1] - 避免使用已弃用的参数(如 `--with-http_dav_module`) - 阿里云环境建议启用 `--with-threads` 和 `--with-file-aio` 提升性能 --- #### **四、处理编译错误** 若出现类似 `ngx_http_request_t` 结构体成员缺失错误[^3]: 1. 打开报错文件(如 `nginx-http-flv-module` 中的 `.c` 文件) 2. 注释掉报错行(例如与 `zero_in_uri` 相关的代码块) 3. 重新执行 `make` 命令 --- #### **五、编译与安装** ```bash make -j$(nproc) sudo make install sudo mkdir /etc/nginx/conf.d ``` 编译完成后,创建配置目录以存放模块配置--- #### **六、注册系统服务** 1. **创建systemd服务文件**(解决[^2]错误): ```bash sudo nano /etc/systemd/system/nginx.service ``` 内容如下: ```ini [Unit] Description=nginx - high performance web server After=network.target [Service] Type=forking PIDFile=/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload ExecStop=/usr/sbin/nginx -s quit [Install] WantedBy=multi-user.target ``` 2. **启动并验证服务**: ```bash sudo systemctl daemon-reload sudo systemctl start nginx sudo systemctl enable nginx ``` --- #### **七、配置RTMP和HTTP-FLV** 在 `/etc/nginx/nginx.conf` 的 `http` 块外添加: ```nginx rtmp { server { listen 1935; chunk_size 4096; application live { live on; meta copy; } } } http { server { listen 80; location /stat { rtmp_stat all; } location /control { rtmp_control all; } } } ``` 执行 `sudo nginx -s reload` 重新加载配置--- #### **八、验证安装** 1. **检查模块加载**: ```bash nginx -V 2>&1 | grep -E 'rtmp|flv' ``` 输出应包含 `--add-module=../nginx-rtmp-module` 和 `--add-module=../nginx-http-flv-module` 2. **测试推流**: ```bash ffmpeg -re -i input.mp4 -c copy -f flv rtmp://服务器IP/live/streamkey ``` --- ### 注意事项 - 阿里云需开放安全组端口(1935/RTMP, 80/HTTP) - 建议使用 `certbot` 配置HTTPS加密直播流 - 若需动态模块,可添加 `--with-compat` 参数 --- ### 相关问题 1. 如何通过FFmpeg实现多分辨率自适应推流? 2. 在Nginx中如何限制RTMP流的并发连接数? 3. 阿里云服务器上如何配置防火墙规则允许RTMP流量? 4. 如何利用HTTP-FLV模块实现低延迟直播播放?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值