nginx的配置详解

nginx的配置文件nginx.conf内放置了所有的这个nginx的配置和可以修改的自定义的参数

这些参数是很多的  需要我们去发掘去寻找

nginx.conf 的一个简单配置

#user  nobody;
worker_processes  1;  # 指定nginx指定启动进程,可以根据cpu核数来优化:

#error_log  logs/error.log;  # 记录nginx的错误日志
#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;
    #                  '$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;

    # 虚拟主机标签段
    server {
        listen       80;  # 监听的端口
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

#        location / {  # 你输入的信息
#            root   html;   # 默认寻找配置
#            index  index.html index.htm;  # 寻找路径下的文件
#        }
        location / {
             root /opt/static/;   # 现在指定这个根路径是/opt/static/
             index index.html inde.html;  # 只要前面的index 后面的有一个对的都去查找
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
View Code

nginx前几天我碰到了一个问题就是你的上传文件的时候 一到大文件就会进行报错  不可以使用这个时候 是因为nginx的一个配置信息

nginx502 这个是上传大文件的时候出错

这个原因一般都是你的参数设置有问题

502 Bad Gateway:作为网关或者代理工作的服务器尝试执行请求时,从上游服务器接收到无效的响应。

我们需要在nginx.conf中的http的参数中设置:

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 2048;
  # 设置大文件上传的参数 client_max_body_size 100m; # 控制全局nginx所有请求报文大小 proxy_connect_timeout 300s; # 连接时间 proxy_send_timeout 300s; # 代理服务器发送时间 proxy_read_timeout 300s; # 等待时间
client_max_body_size :  控制上传文件大小

proxy_connect_timeout: 定义一个nginx 与real server 建立链接的超时时间,通常不要超过75秒.默认:60s

 proxy_send_timeout:  定义一个nginx向 real server发送请求的超时时间,超时只在两次连续写入操作之间设置, 而不是用于传输整个请求,如果real server在此时间内没有收到任何内容,则连接将关闭。

proxy_send_timeout:定义一个nginx向 real server发送请求的超时时间,超时只在两次连续写入操作之间设置, 而不是用于传输整个请求,如果real server在此时间内没有收到任何内容,则连接将关闭。

具体配置:

  

http服务器
与提供http服务相关的一些配置参数。例如:是否使用keepalive啊,是否使用gzip进行压缩等。

sendfile on
开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,减少用户空间到内核空间的上下文切换。对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。

keepalive_timeout 65 : 长连接超时时间,单位是秒,这个参数很敏感,涉及浏览器的种类、后端服务器的超时设置、操作系统的设置,可以另外起一片文章了。长连接请求大量小文件的时候,可以减少重建连接的开销,但假如有大文件上传,65s内没上传完成会导致失败。如果设置时间过长,用户又多,长时间保持连接会占用大量资源。

send_timeout : 用于指定响应客户端的超时时间。这个超时仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接。

client_max_body_size 10m
允许客户端请求的最大单文件字节数。如果有上传较大文件,请设置它的限制值

client_body_buffer_size 128k
缓冲区代理缓冲用户端请求的最大字节数
模块http_proxy:
这个模块实现的是nginx作为反向代理服务器的功能,包括缓存功能(另见文章)

proxy_connect_timeout 60
nginx跟后端服务器连接超时时间(代理连接超时)
proxy_read_timeout 60
连接成功后,与后端服务器两个成功的响应操作之间超时时间(代理接收超时)

proxy_buffer_size 4k
设置代理服务器(nginx)从后端realserver读取并保存用户头信息的缓冲区大小,默认与proxy_buffers大小相同,其实可以将这个指令值设的小一点

proxy_buffers 4 32k
proxy_buffers缓冲区,nginx针对单个连接缓存来自后端realserver的响应,网页平均在32k以下的话,这样设置

proxy_busy_buffers_size 64k
高负荷下缓冲大小(proxy_buffers*2)

proxy_max_temp_file_size
当 proxy_buffers 放不下后端服务器的响应内容时,会将一部分保存到硬盘的临时文件中,这个值用来设置最大临时文件大小,默认1024M,它与 proxy_cache 没有关系。大于这个值,将从upstream服务器传回。设置为0禁用。

proxy_temp_file_write_size 64k
当缓存被代理的服务器响应到临时文件时,这个选项限制每次写临时文件的大小。proxy_temp_path(可以在编译的时候)指定写到哪那个目录。
nginx 对文件限制的详解

nginx的负载均衡池子:

1、proxy_pass    # 负载一个地址

2、upstream   #可以负载多个地址

出了上面的proxy_pass可以做负载均衡之外 还可以使用upstream 定义一个负载均衡池

具体是在ngin.conf中修改配置:

 

 我可以现在server外http内定义一个upstream  +下面要指定的地址名字 

upstream  + 要转发的地址名{
    server +要转发的ip端口   weight=10;(weight=10代表最高权重 可以加或者不加)
    
}

下面要转发的时候直接转发到这个定义的名字上就可以了




  upstream django{
        server 0.0.0.0:9000 weight=10;
        }
    # 虚拟主机标签段  ,这里定义mytb.com
    server {
        listen       80;  # 监听的端口
        #server_name  www.mytb.com;
        server_name  localhost;

        location / {
             #deny 60.168.97.34;
             #deny 192.168.20.91;
             #proxy_pass http://127.0.0.1:9000;
             #



            include  /opt/nginx1-12/conf/uwsgi_params;   # 分发到这个自带的配置文件内
            #uwsgi_pass  0.0.0.0:9000;   # 然后指定分发uwsgi启动的端口
            uwsgi_pass django;
             #root /opt/static/mytb/;
             #index index.html inde.html;  
        }

然后再重启nginx  就可以使用了

原文地址:https://www.cnblogs.com/zhaoyunlong/p/10275397.html