【Nginx基础篇】nginx的基本配置解析和应用场景

目录

一、最小配置

二、虚拟主机


一、最小配置

原始的配置文件


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

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

}

将注释删除后最简版


worker_processes  1;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;


    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;


        location / {

            root   html;
            index  index.html;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}
worker_processes
worker_processes 1 ; 默认为 1 ,表示开启一个业务进程
worker_connections
worker_connections 1024 ; 单个业务进程可接受连接数
include mime.types;
include mime.types ; 引入 http mime 类型
default_type application/octet-stream;
default_type application/octet - stream ; 如果 mime 类型没匹配上,默认使用二进制流的方式传输。
sendfifile on;
sendfile on ; 使用 linux sendfile(socket, file, len) 高效网络传输,也就是数据 0 拷贝。
未开启 sendfifile,此时nginx从磁盘拷贝请求资源后,到网络接口缓存之间仍需要经过多次拷贝

 开启后,只需要一个信号,读取一次

 keepalive_timeout 65;  连接保持心跳时间,这里分为客户端和被代理端,以后会介绍

server,一个server可以表示一台主机,所以配置多个server可以有多态虚拟主机(vhost)

 虚拟主机配置

server {
    listen 80; 监听端口号
    server_name localhost; 主机名
    location / { 匹配路径
        root html; 文件根目录
        index index.html index.htm; 默认页名称
    }
    error_page 500 502 503 504 /50x.html; 报错编码对应页面
    location = /50x.html {
        root html;
    }
}

二、虚拟主机

原本一台服务器只能对应一个站点,通过虚拟主机技术可以虚拟化成多个站点同时对外提供服务
servername 匹配规则
我们需要注意的是 servername 匹配分先后顺序,写在前面的匹配上就不会继续往下匹配了。
完整匹配
我们可以在同一 servername 中匹配多个域名
server_name vod.mmban.com www1.mmban.com;
通配符匹配
server_name *.mmban.com
通配符结束匹配
server_name vod.*;
正则匹配
server_name ~^[0-9]+\.mmban\.com$;

猜你喜欢

转载自blog.csdn.net/m0_62946761/article/details/130441450