nginx conf配置篇,如何配置.conf文件

这篇具有很好参考价值的文章主要介绍了nginx conf配置篇,如何配置.conf文件。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Nginx的主配置文件是nginx.conf,这个配置文件一共由三部分组成,分别为全局块、events块和http块。
在http块中,又包含http全局块、多个server块。每个server块中,可以包含server全局块和多个location块。在同一配置块中嵌套的配置块,各个之间不存在次序关系。

一、默认配置文件


#user  nobody;
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;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   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   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;
    #    }
    #}

}

二、结构说明

...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}

1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。

2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。

5、location块:配置请求的路由,以及各种页面的处理情况。

三、配置说明

########### 每个指令必须有分号结束。#################
#配置用户或者组,默认为nobody nobody,以什么用户启动 nginx程序,涉及到文件的读写权限,一般应该用root。
user root;  

#允许生成的进程数,默认为1。标配一个cpu起一个进程。
worker_processes auto;  

#指定nginx进程运行文件存放地址
pid /nginx/pid/nginx.pid;   

#制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
#error_log log/error.log debug;  

events {
	#设置网路连接序列化,防止惊群现象发生,默认为on
    #accept_mutex on;  
     
    #设置一个进程是否同时接受多个网络连接,默认为off
    #multi_accept on; 
    
    #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    #use epoll;     
    
    #最大连接数,可以根据实际情况扩大,生产可扩大到10240
    worker_connections  10240;    
}


http {
	#文件扩展名与文件类型映射表
    include       mime.types; 
      
    #默认文件类型,默认为text/plain
    default_type  application/octet-stream; 
    
    #取消服务日志
    #access_log off; 
    
    #自定义格式    
    #log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; 
    
    #combined为日志格式的默认值
    #access_log log/access.log myFormat;  

	#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    sendfile on; 
    
    #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
    sendfile_max_chunk 100k;  
    
    #连接超时时间,默认为75s,可以在http,server,location块。
    keepalive_timeout 75;  
    
    #接收客户端请求体超时
    client_body_timeout 20s;
    
    #客户端连接nginx超时, 建议5s内,接收客户端header超时时间,超时返回408
    client_header_timeout 10s;

	#限制请求体的大小,若超过所设定的大小,返回413错误。
    client_max_body_size 10M;


	#Nginx分配给请求数据的Buffer大小,如果请求的数据小于client_body_buffer_size直接将数据先在内存中存储。
	#大于该值小于client_max_body_size,就会将数据先存储到临时文件(用户组要有读写权限)中,client_body_temp 指定的路径中,默认该路径值是/tmp/。
    client_body_buffer_size 128k;
    
    #开启自定义错误重定向,不存在会返回404页面
    fastcgi_intercept_errors on;

	#热备
    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  
    }

    server {
    	#单连接请求上限次数。
        keepalive_requests 120; 
        #监听端口
        listen       4545;   
        #监听地址 
        server_name  127.0.0.1;    
        #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。     
        location  ~*^.+$ {   
        	#根目录,设置目录
           #root path;  
           #设置默认页
           #index index.htm;  
           #请求转向mysvr 定义的服务器列表
           proxy_pass  http://mysvr;  
            #拒绝的ip
           #deny 127.0.0.1; 
            #允许的ip
           #allow 172.18.5.54;           
        } 
    }
}

四、常用案例

案例1:页面转发,80端口指向index,前后台可用

 server {
        listen       80;
        server_name  localhost;
        location / {
            root   /home/user01/dist/;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }

案例2:接口转发。80/api指向后台服务端口

server {
		location /api/{
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:48080/;
          }
}

案例3:文件路径转发。80/uploadFile指向文件路径

server {  
		location /uploadFile/{
            proxy_pass http://localhost:48081/;
        }  
}

   server {  
        listen         48081;        
        server_name  localhost;   
        charset utf-8; 
        root    /home/user01/java/file; 
        location / {
            autoindex on;             
            autoindex_exact_size off; 
            autoindex_localtime on;  
        } 
   }

简略配置:

location /uploadFile{
 		root    /home/user01/java/file; 
}

案例4::端口转发,将本地端口指向其他地址

server {
	listen 127.0.0.1:8279;
	location / { 
		proxy_pass http://ip:8279;
	}
}

案例5:负载均衡,将一个端口指向多个服务

server {
		listen 8888;
		location / { 
			#通过代理将请求发送给 upstream 命名的HTTP 服务
			proxy_pass http://myserver;
		}
	}
	
#定义一个 HTTP 服务组
upstream  myserver{
		#用server定义HTTP 地址。后面不写默认轮询。
		server 127.0.0.1:8080 max_fails=5 fail_timeout=10s weight=10;
		server 127.0.0.1:8081 backup;
		server 127.0.0.1:8082 down;
	}
upstream  myserver{
	#相同的客户端ip请求相同的服务器。
	server 127.0.0.1:8080;
	server 127.0.0.1:8081;
	ip_hash;
}
  • weight,加权轮询。
  • down,表示当前的server暂时不参与负载均衡。
  • backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。
  • max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
  • fail_timeout,在经历了max_fails次失败后,暂停服务的时间。max_fails可以和fail_timeout一起使用。

五、配置文件新增端口转发配置技巧

1、在nginx.conf中http加入一行include tcpconf/*.conf;

http {
include tcpconf/*.conf;
}

2、在nginx.conf目录下,新建文件夹tcpconf,其他配置文件命名.conf,文件中写入转发的配置。例如下面,然后重启生效。文章来源地址https://www.toymoban.com/news/detail-786733.html

server {
    listen 127.0.0.1:8279;
   location / { 
   proxy_pass http://ip:8279;
	}
}

到了这里,关于nginx conf配置篇,如何配置.conf文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Nginx网站服务详解(Nginx服务的主配置文件 ——nginx.conf)

    目录 一、全局配置的六个模块简介 二、Nginx配置文件的详解 1)全局配置模块  2)I/O 事件配置  3)HTTP 配置  4)web服务监听设置 5)其他设置 location常见配置指令:“root、alias、proxy_pass 对比:  当设置  location /test{     },alias /var/www/html  和   root /var/www/html  有什么区别

    2024年02月07日
    浏览(36)
  • 一文彻底搞懂Nginx的.conf文件路径配置

    前言:最近在Nginx上部署Vue项目,同时还存了一些静态资源,但我查了好久都没找到一篇详细介绍路径配置的文章,因此就根据我有限的经验写了这篇文章,希望能够对读者有所帮助 如下图所示,这是我配置好的一个server代码块,我这里配置了https,所以会比默认的多一部分

    2024年02月09日
    浏览(19)
  • 【ubuntu】ubuntu 20.04安装docker,使用nginx部署前端项目,nginx.conf文件配置

    docker 官网:Install Docker Engine on Ubuntu 1.将apt升级到最新 2.使用apt安装 docker 和 docker-compose (遇到提示输入 y ) 3.将当前用户添加到docker用户组 4.运行hello-world 运行成功 1.修改配置文件 修改conf/nginx.conf 2.重新挂载 给容器设置自启动(如果提示就去掉sudo) 给docker设置开机自启动

    2024年01月20日
    浏览(29)
  • Mac如何打开隐藏文件中Redis的配置文件redis.conf

    Redis下载 cd /usr open local 3.找到redis.conf并打开,即可修改配置信息    

    2024年02月12日
    浏览(18)
  • Nginx配置 多个域名指向同一个服务器文件

    因为公司开发方面有响应的需求,需要多个域名指向同一个服务器下的文件(即访问的域名不同但访问的服务器下的文件是同一个) 已经过解析 并且指向同一个ip地址的多个域名 服务器nginx已配置好 如果有需求需要给域名配置ssl证书(https) 找到服务器的nginx.conf文件 一般情况

    2024年02月11日
    浏览(29)
  • linux中如何使用nginx部署多个静态资源文件?

    一、切换到nginx的配置文件路径下:cd /usr/local/nginx/conf 二、编辑nginx的配置文件:vim nginx.conf 三、修改或添加location代码块,如下 location /project1{ alias /usr/local/nginx/dist#静态资源路径 index index.html index.htm; } location /project2{ alias /usr/local/nginx/dist#静态资源路径 index index.html index.htm

    2024年02月05日
    浏览(16)
  • 2022-03-15 【srs流媒体】rtmp超时中断场景解决,如何增加srs配置文件conf的配置项

    srs媒体服务器功能很强大,配置很方便。但是真正遇到问题的时候,在网上搜索了一圈,发现资料基本都是停留表面,没有人深入去讲,或者没有人完全讲到要点。 本人结合项目使用过程中出现的问题,给大家讲一下常用的一些功能点,绝对是干货。欢迎大家关注。 项目相

    2023年04月09日
    浏览(13)
  • Windows系统如何修改Nginx配置实现远程访问多个本地站点

    1. 下载windows版Nginx 进入官方网站(http://nginx.org/en/download.html)下载windows版的nginx 下载好后解压进入nginx目录双击Nginx.exe即可运行 打开浏览器输入http://localhost ,nginx默认使用80端口,所以无需加端口号.出现nginx欢迎页面表示启动成功 2. 配置Nginx 在8086,和9999端口分别配置两个web服务

    2024年01月22日
    浏览(20)
  • nginx 多配置(.conf)的使用

    通常情况下我们在一个.conf 承载好多服务代理的配置,使用.conf 文件过大,过长,以至于管理难,有时修改某个小配置,由于重起或重截配置文件,使用服务受影响。因此使用多配置组合的方式进行管理很有必要。 注意:本文中配置的文件和影射的目录文件,因为我用的是

    2024年02月11日
    浏览(25)
  • Nginx配置文件修改结合内网穿透实现公网访问多个本地web站点

    1. 下载windows版Nginx 进入官方网站(http://nginx.org/en/download.html)下载windows版的nginx 下载好后解压进入nginx目录双击Nginx.exe即可运行 打开浏览器输入http://localhost ,nginx默认使用80端口,所以无需加端口号.出现nginx欢迎页面表示启动成功 2. 配置Nginx 在8086,和9999端口分别配置两个web服务

    2024年04月10日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包