美文网首页
Nginx学习之Nginx.conf配置文件详解

Nginx学习之Nginx.conf配置文件详解

作者: 扶得一人醉如苏沐晨 | 来源:发表于2023-09-25 12:56 被阅读0次
image.png

nginx.conf 文件详解

1、下面是一个 nginx.conf 配置文件的基本结构:

image.png image.png

2、nginx.conf 配置文件内容介绍如下:

#创建进程的用户和用户组,多个时用空格隔开
#user  nobody;
#服务进程数量,一般等于 CPU 数量
worker_processes  1;
 
#全局错误日志定义,建议开启error级别日志.[ debug | info | notice | warn | error | crit ]
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#记录进程 ID 的文件
#pid        logs/nginx.pid;
 
events {
    #一个 worker_processe 允许的最大并发连接数量
    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;
    #http连接的持续时间
    keepalive_timeout  65;
 
    #gzip压缩设置,开启或关闭
    #gzip  on;
 
    #设定负载均衡的服务器列表,可以设置多个 upstream,使用不同的名称 wmx 区分即可
    #upstream 默认是没有,需要自己添加
    upstream wmx {
        #weigth参数表示权值,权值越高被分配到的几率越大
        server 127.0.0.1:8081 weight=5;
        server 127.0.0.1:8082 weight=5;
        server 127.0.0.1:8083 weight=5;
    }
 
    server {
        #nginx监听的端口号
        listen       80;
        #当前 server(虚拟主机)的域名,可以有多个,用空格隔开
        server_name  localhost;
        #字符编码方式
        #charset koi8-r;
 
        #设定本虚拟主机的访问日志。关闭日志可以减少IO,提高性能。
        #access_log  logs/host.access.log  main;
 
        #默认请求
        location / {
            #定义服务器的默认网站根目录位置
            root   html;
            #定义首页索引文件的名称
            index  index.html index.htm;
 
            #请求转向 wmx 定义的服务器列表
            proxy_pass http://wmx
        }
 
        #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;
    #    }
    #}
 
}

nginx 内置全局变量

1、Nginx 作为一个成熟、久经考验的负载均衡软件,与其提供丰富、完整的内置变量是分不开的,它极大增加了对 Nginx 网络行为的控制细度。

2、这些变量大部分都是在请求进入时解析的,并把他们缓存到请求 cycle 中,方便下一次获取使用。

3、下面是 nginx 配置中常用的内置全局变量,可以在配置的任何位置使用它们

image.png
image.png
image.png

nginx 常用命令

start nginx.exe //启动
nginx.exe -s reload //重启
nginx.exe -s stop //快速停止
nginx.exe -s quit //完整有序停止

没配置环境变量时,在 nginx.exe 所在目录下使用命令行即可。

location 解析过程

image.png image.png

相关文章

网友评论

      本文标题:Nginx学习之Nginx.conf配置文件详解

      本文链接:https://www.haomeiwen.com/subject/bytcbdtx.html