nginx配置文件,配置虚拟主机

HTTP协议

HTTP是客户端与服务端之间请求和应答的标准; 全名叫做: 超文本传输协议; HTTP协议在客户端与服务端传输数据的过程中规定下层必须是可靠链接, 即TCP链接建立过程;

http request
HTTP消息体, 响应头, 请求头
  • HTTP协议定义Web客户端如何从Web服务器请求Web页面, 以及服务器如何把Web页面传送给客户端. HTTP协议采用了请求/响应模型. 客户端向服务器发送一个请求报文, 请求报文包含请求的方法、URL、协议版本、请求头部和请求数据. 服务器以一个状态行作为响应, 响应的内容包括协议的版本、成功或者错误代码、服务器信息、响应头部和响应数据
http ask
  • 请求头部构成
request function
request function example
  • URL及URI

    URL叫做统一资源定位符; 就是我们俗称的网址

    URI叫做统一资源标识符; 用来标定各个资源的抽象

  • 响应头部构成

    ask headers structure
    ask headers example
  • 所有HTTP响应的第一行都是状态行, 依次是当前HTTP版本号, 3位数字组成的状态代码, 以及描述状态的短语,彼此由空格分隔; 命令行中常用curl -I DaemonName来获取服务器的状态; 例如"200 OK", "404 Not Found", 但是WEB开发者仍然能够自行决定采用何种短语, 用以显示本地化的状态描述或者自定义信息;

    status code

Nginx架构:

  • 请求的处理流程


    image.png
  • 编译安装

[root@nignxwebsite ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
--2019-12-01 11:58:48--  http://nginx.org/download/nginx-1.16.1.tar.gz
正在解析主机 nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3
正在连接 nginx.org (nginx.org)|95.211.80.227|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1032630 (1008K) [application/octet-stream]
正在保存至: “nginx-1.16.1.tar.gz”

100%[==============================================================================>] 1,032,630    188KB/s 用时 6.1s

2019-12-01 11:58:55 (164 KB/s) - 已保存 “nginx-1.16.1.tar.gz” [1032630/1032630])

[root@nignxwebsite nginx-1.16.1]# groupadd -g 996 nginx
[root@nignxwebsite nginx-1.16.1]# useradd -u 996 -g 998 -s /sbin/nologin -M nginx
[root@nignxwebsite nginx-1.16.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

报错处理:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.
解决方案:
[root@nignxwebsite nginx-1.16.1]# yum -y install pcre-devel

错误处理:
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib= option.
解决方案:
[root@nignxwebsite nginx-1.16.1]# yum -y install zlib-devel

[root@nignxwebsite nginx-1.16.1]# make && make install
[root@nignxwebsite nginx-1.16.1]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginxctl
[root@nignxwebsite nginx-1.16.1]# nginxctl (启动)
[root@nignxwebsite nginx-1.16.1]# nginxctl -s stop (停止)
[root@nignxwebsite nginx-1.16.1]# nginxctl -s reload (优雅的停止后启动)
[root@nignxwebsite nginx-1.16.1]# nginxctl -t (检查配置文件的语法)

启动后观察到如下页面即代表成功:


image.png
  • 配置文件结构

    #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;
        #    }
        #}
    }
    
  • 基础配置指令

    • user: 指定运行nginx worker进程的用户身份;
    • worker_process: 指定CPU亲和性, 通常与本机的CPU逻辑核心数相同, 以便更好地利用CPU;
    • error_log: 指定错误日志的存储位置;
    • pid: 指定nginx运行时pid的存储位置;
    • events: 指定事件处理模型, 即上方叙述的状态机, 每个状态机决定处理的事件流
      • use: 指定使用的事件模型, 一般指定epoll异步非阻塞事件模型;
      • worker_connections: 描述worker进程处理的最大连接数, 通常和系统的最大文件打开数相同;
    • http: 指定虚拟主机server块, 日志格式, 缓存, 压缩方式, 长连接, 文件类型及类型映射表等
      • include: 设置包含文件及文件类型;
      • default_type: 默认文件类型;
      • log_format: 指定日志格式;
      • access_log: 指定日志存储位置及开启状态;
      • send_file: 文件传输状态;
      • keepalive_timeout: 长连接保持时间;
      • tcp_nopush: tcp连接返回确认接收信息;
      • gzip: 文件传输时压缩与否;
      • server: 设置响应用户的文件位置及响应用户请求的方式
        • listen: 设置监听端口;
        • server_name: 设置域名;
        • charset: 设置字符集;
        • location: 指定页面路由位置, 及指定用户访问首页时加载的文件, 可配置变量,正则等;
        • error_page: 指定错误页面加载位置;
  • nginx虚拟主机

    #### -.- file: /usr/local/nginx/conf/nginx.conf -.- ####
    user  nobody;
    worker_processes  1;
    error_log  logs/error.log;
    pid        logs/nginx.pid;
    
    events {
        use epoll;
        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  65;
        include /usr/local/nginx/conf.d/*.conf;
    }
    
    #### -.- file: /usr/local/nginx/conf.d/vhost_daemon.conf -.- ####
    server {
       listen       80;
       server_name  www.daemons.com;
       charset koi8-r;
       access_log  logs/daemons.access.log  main;
    
       location / {
           root   html;
           index  index.html index.htm;
       }
    
       error_page   404              /404.html;
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   html;
       }
    
       location ~ \.php$ {
           proxy_pass   http://127.0.0.1;
       }
    
       location ~ \.php$ {
           root           html;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           include        fastcgi_params;
       }
    }
    
    • 单台主机实现多个nginx网站配置
     就是在虚拟主机配置目录中写多个不同域名的虚拟主机配置文件;
    

你可能感兴趣的:(nginx配置文件,配置虚拟主机)