【Nginx】Nginx 配置文件详解,一文让你了解 Nginx 配置文件

1. nginx

Nginx (engine x) 是一个高性能的 HTTP 和反向代理 web 服务器,同时也提供了 IMAP/POP3/SMTP 服务。

2. nginx 配置文件目录

  • fastcgi.conf:存放fastcgi 相关的配置
  • fastcgi.conf.default:fastcgi.conf 的原始备份文件,用于还原
  • fastcgi_params:fastcgi 相关参数文件
  • fastcgi_params.default:fastcgi_params 的原始备份文件,用于还原
  • koi-utf:编码转换映射文件
  • koi-win:编码转换映射文件
  • mime.types:存放媒体资源的类型,例如:xml、html、css
  • mime.types.default:mime.types 的原始备份文件,用于还原
  • nginx.conf:nginx 默认主配置文件
  • nginx.conf.default:nginx.conf 的原始备份文件,用于还原
  • scgi_params:与fastcgi_params一样,传递哪些服务器的变量
  • scgi_params.default:scgi_params 的原始备份文件,用于还原
  • uwsgi_params:服务器和服务端应用程序的通信协议,规定了怎么把请求转发给应用程序和返回
  • uwsgi_params.default:uwsgi_params 的原始备份文件,用于还原

3. nginx 配置文件常用配置(nginx.conf)

nginx配置文件主要分为六个区域

  • main:全局设置(作用域全局)
  • events:nginx工作模式
  • http:http设置
    • upstream:负载均衡服务器设置
      • server:主机设置(一个server节点就是一个虚拟机)
        • location :URL配置

1.main主配置区域,对应nginx 核心模块的功能

#main区域,主配置区域,全局的

#定义 www 运行的用户和用户组,建议新建 www 用户和用户组 
user  www;

#启动工作进程数数量,一般设置和CPU核心数一致
worker_processes  1;    

#全局错误日志定义类型.【debug|info|notice|warm|error|crit】
error_log  logs/error.log;   
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#进程pid文件位置
#pid        logs/nginx.pid;    

3.2 events区域

#events区域
events {
    
    
    #一个进程可以产生多少个工作的连接,nginx的最大并发访问量
    worker_connections  1024; 
    
    #使用epoll 模型,异步IO 处理模型,epoll 模型没有1024 的限制,并发发访问量特别快
    use epoll;    
}

3.3 nginx 核心模块常用配置参数


http {
    
    
    #包含媒体资源类型的文件,调用mime.types文件.
    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;
    
    #作为web服务器的时候打开sendfile加快静态文件传输
    sendfile        on;
    
    #在开启了sendfile的情况下,合并请求后统一发送给客户端。
    tcp_nopush     on;    
    
    #在开启了keepalived模式下的连接是否启用TCP_NODELAY选项,当为off时,延迟0.2s发送,默认On时,不延迟发送,立即发送用户相应报文。
    tcp_nodelay  off;
    
    #长连接超时时间,单位是秒
    keepalive_timeout  65;   
    
    #设置了每个散列桶占用的内存大小
    types_hash_max_size 2048;
    
    #开启压缩功能.
    gzip  on;
    
    #压缩的缓冲区大小
    gzip_buffers 16 8k;
    
	#压缩级别,默认为1
    gzip_comp_level 6;
    
	# 禁止那些浏览器压缩功能
	gzip_disable "MSIE [1-6].(?!.*SV1)";
   
	#小于128字节不压缩
	gzip_min_length 128;
    
    #只对 那些http版本进行压缩 默认 1.1 
	gzip_http_version 1.1;
    
 
    #用来配置虚拟主机,每一个server 段都是一个虚拟主机
    server {
    
    
        #监听80 端口,可以做基于端口的虚拟主机,listen 后面 IP加端口就是基于IP的虚拟主机
        listen       80; 
        
        #用来定义虚拟主机名称即域名; 支持*通配符,支持~起始字符做正则表达式匹配
        server_name  localhost;
        
		#设置编码格式,默认是俄语格式,可以改为utf-8
        charset utf-8;
        
 		#定义虚拟主机的访问日志
        access_log  /var/log/nginx/host.access.log  main;
 
        ###localtion 匹配段,用于匹配
        location / {
    
    
            #设置 WEB 资源路径映射,用于指明用户请求的 uri 所对应的本地文件系统上的文档目录.
            root   /usr/share/nginx/html;
            
            #默认资源设置
            index  index.html index.htm;
        }
          
 		#当客户端访问出错时定义返回的页面, 跳转404页面 /404.html;   
        #error_page  404              
        # 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 api scripts to Apache listening on xxx.xxx.xxx.xxx
        location /api/ {
    
    
            
            # webapi服务的IP地址和端口,内网地址
            proxy_pass         http://xxx.xxx.xxx.xxx;
            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;
            proxy_set_header   X-Forwarded-Port  $server_port;
            
			#此指令设置nginx能处理的最大请求主体大小
            client_max_body_size 200m;
            #接超时时间
            proxy_read_timeout  500;
        }

      
        # 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.conf;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
    
    
        #    deny  all;
        #}
    }
 

4.nginx 前端怎么配置

在nginx目录下添加一个html文件做静态文件托管,端口为8080

将前端项目复制到/nginx/html文件夹下
将nginx下的配置文件(/nginx/conf/nginx.conf)里的添加一个server节点配置改为要配置的域名

server {
    
    
        #监听的端口,
        listen       8080;
    
        #此处localhost可改为要访问的域名或者ip地址
        server_name  localhost;
    
        #编码
        charset utf-8;

        #access_log  logs/host.access.log  main;
    
        #error_page  404              /404.html;

        location / {
    
    
            #nginx下HTML文件夹,访问上述域名时会检索此文件夹下的文件进行访问
            root   html;
            #输入网址(server_name:port)后,默认的访问页面
            index  index.html index.htm;
        }
}

配置正确后,重启nginx(service nginx restart )

service nginx restart

测试访问

5.nginx代理后端API

如果有跨域问题。可以添加代理配置(用/api替换了//xxx.xxx.xxx.xxx。)

location /api/ {
    
    
    # webapi服务的IP地址和端口,内网地址
    proxy_pass         http://xxx.xxx.xxx.xxx;
    
    #重定义发往后端服务器的请求头
    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;
    proxy_set_header   X-Forwarded-Port  $server_port;

    client_max_body_size 200m;
    proxy_read_timeout  500;
}

6.nginx查看服务状态

nginx中需要配置开启Nginx Status来查看服务器运行状态

开启nginx status

server {
    
    
        #监听的端口,
        listen       8080;
        
        ···

        location /status {
    
    
            stub_status on;
            access_log off;            
        }
    
        ····
}

重启nginx 操作命令比较简单,请依照你的环境重启你的nginx即可。
打开status页面 在浏览器中输入nginx的地址:xxx.xxx.xxx.xxx/status,即可查看…

Active connections: 4 
server accepts handled requests
 4 4 15 
Reading: 0 Writing: 1 Waiting: 3 

Active connections: 4 
表示Nginx正在处理的活动连接数4个。

server accepts handled requests
 4 4 15 
第一个server表示Nginx启动到现在共处理了4个连接
第二个accepts表示Nginx.启动到现在共成功创建4次握手
第三个handled requests表示总共处理了15次请求

Reading: 0 Writing: 1 Waiting: 3 
Reading:读取到客户端的Header信息数
Writing:返回给客户端Header信息数
Waiting:已经处理完正在等候下一次请求指令的驻留链接(开启keep-alive的情况下,这个值等于Active-(Reading+Writing)

7.nginx怎么看日志

1. nginx查看访问日志

  1. 在服务器部分或HTTP中使用access_log指令启用访问日志。

默认情况下,访问日志是在Nginx配置文件中定义的。因此,所有虚拟主机的访问日志将存储在同一配置文件中。

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;
      #关闭指令
      #access_log off
      ...
}

2.建议通过记录到新的单独文件中来分开所有虚拟主机的访问日志。

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;
   
       server {
    
    
                  listen 80;
                  Server_name example.com
                  access_log  /var/log/nginx/example.access.log;
                  ...
                  ...
                }
}

3.对nginx的配置进行所有更改后,重新加载nginx,然后运行tail命令

vim /var/log/nginx/example.access.log

2. nginx查看错误日志

如果nginx突然停止运行或无法正常工作,它将在错误日志中记录所有事件。因此,使用错误日志,您可以找到更多详细信息。它还记录警告,但无法识别已发生的问题。
nginx错误日志的有不同的安全级别,可以在错误日志中使用以下安全级别:
emerg:当系统不稳定时,用于紧急消息
alert:生成严重问题的警报消息。
crit:用于紧急情况下立即处理。
error:处理页面时,可能会发生错误。
warn:用于警告消息
notice:您也可以忽略的通知日志。
info:有关信息,消息
debug:指向用于调试信息的错误位置。

启用错误日志

http {
    
    
       ...
       ...
       error_log  /var/log/nginx/error_log;
       #关闭指令
       #error_log off
    
       server {
    
    
                listen 80;
                server_name example1.com;
                    error_log  /var/log/nginx/example1.error_log  warn;
                        ...
       }
       server {
    
    
                listen 8081;
                server_name example2.com;
                    error_log  /var/log/nginx/example2.error_log  debug;
                        ...
   }
}

nginx -t 检查一下Nginx是否有误,没报错后方可进行重启

nginx -t 

重新加载nginx

service nginx restart

然后运行页面 报错了直接进入错误日志中进行查看

vim var/log/nginx/example1.error_log  

猜你喜欢

转载自blog.csdn.net/ihero/article/details/132355115