Nginx配置文件nginx.conf解读

简介: Nginx配置文件nginx.conf解读

正文


打开一个全新配置的nginx的nginx.conf文件,文件结构大概是这个样子:

#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
        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;
        #}
    }
    #
    #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;
    #    }
    #}
}


一大堆代码看起来眼花缭乱,其实并不复杂,我们来看精简版的:

# 全局区
worker_processes  1;  # 有1个工作的worker子进程
#可以自行修改,但太大会造成相互争夺CPU,一般来说:子进程数 = CPU数 * 核数
# 配置nginx的连接特性
events {
    worker_connections  1024; # 1个worker子进程能同时允许的最大连接数
}
# http服务器主要段
http {
    # 设定mime类型,类型由mime.types文件定义
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on; # 是否调用sendfile来输出文件
    keepalive_timeout  65; # 连接超时时间
    # 虚拟主机段
    server {
        listen       80; # 监听端口
        server_name  localhost; # 监听主机\域名\端口
    # 定义访问响应
        location / {
            root   html; # 根目录定位
            index  index.html index.htm; # 默认访问页面定位
        }
    # 定义错误提示页面
        location = /50x.html {
            root   html;
        }
}
相关文章
|
应用服务中间件 Linux 网络安全
Centos 8.0中Nginx配置文件和https正书添加配置
这是一份Nginx配置文件,包含HTTP与HTTPS服务设置。主要功能如下:1) 将HTTP(80端口)请求重定向至HTTPS(443端口),增强安全性;2) 配置SSL证书,支持TLSv1.1至TLSv1.3协议;3) 使用uWSGI与后端应用通信(如Django);4) 静态文件托管路径设为`/root/code/static/`;5) 定制错误页面(404、50x)。适用于Web应用部署场景。
1326 87
|
缓存 负载均衡 应用服务中间件
nginx的配置文件详解
本文详细解释了nginx配置文件中的关键指令和区块,如http、server、location、upstream、events等,并通过一个示例配置文件展示了如何设置HTTP服务器、gzip压缩、反向代理、URL重写、错误页面和负载均衡等,强调了配置的灵活性和实际应用。
1415 4
|
网络协议 前端开发 应用服务中间件
nginxconf.sh 自动生成 nginx tcp 转发配置文件 conf
该脚本由 eisc.cn 开发,用于自动生成 Nginx 代理配置。它根据预设的域名、IP 和端口信息,为多个项目(如 www、work、sou 等)创建对应的 Nginx 配置文件,设置前端转发、端口对端口及后端转发规则,并生成日志和 301 跳转配置。支持自动创建 CGI 解析目录,确保各项目能够正确访问。运行时需具备 root 权限或使用 sudo。
638 9
|
负载均衡 应用服务中间件 Linux
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
这篇博客文章详细介绍了Nginx的下载、安装、配置以及使用,包括正向代理、反向代理、负载均衡、动静分离等高级功能,并通过具体实例讲解了如何进行配置。
1075 5
nginx学习,看这一篇就够了:下载、安装。使用:正向代理、反向代理、负载均衡。常用命令和配置文件,很全
|
域名解析 网络协议 应用服务中间件
nginx server_name配置文件覆盖不生效
nginx server_name配置文件覆盖不生效
|
应用服务中间件 nginx
nginx 配置文件
nginx 配置文件
|
缓存 负载均衡 算法
nginx学习:配置文件详解,负载均衡三种算法学习,上接nginx实操篇
Nginx 是一款高性能的 HTTP 和反向代理服务器,也是一个通用的 TCP/UDP 代理服务器,以及一个邮件代理服务器和通用的 HTTP 缓存服务器。
938 0
nginx学习:配置文件详解,负载均衡三种算法学习,上接nginx实操篇
|
应用服务中间件 网络安全 nginx
nginx作为web服务以及nginx.conf详解
nginx作为web服务以及nginx.conf详解
|
运维 应用服务中间件 网络安全
运维系列.Nginx配置文件结构功能总结
运维系列.Nginx配置文件结构功能总结
628 0
运维系列.Nginx配置文件结构功能总结
|
Ubuntu 应用服务中间件 Linux
Linux学习之Ubuntu 20中OpenResty的nginx目录里内容和配置文件
总的来说,OpenResty的Nginx配置文件是一个强大的工具,它允许你以非常灵活的方式定义你的Web服务的行为。
903 2