目的:本文重点查看并分析nginx默认配置;修改index.html文件输出"Hello World"。

 

 

查看主配置文件

[root@VM_16_8_centos nginx]# vim /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

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  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

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

 

分析配置文件


全局变量

user nginx;    #使用的用户
worker_processes auto;    #工作进程数,自动调整(手动设置的话一般和CPU核数一样)
error_log /var/log/nginx/error.log;    #错误日志文件
pid /run/nginx.pid;        #pid存放路径


事件配置

events {
    worker_connections 1024;    #允许连接数
}


http参数

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  main;        #连接日志`main`的路径

    sendfile            on;        #开启高效传输模式
    tcp_nopush          on;        #防止网络阻塞:配置nginx在一个包中发送全部的头文件,而不是一个一个发送。
    tcp_nodelay         on;        #配置nginx不要缓存数据,快速发送小数据。
    keepalive_timeout   65;        #客户端请求头读取超时时间
    types_hash_max_size 2048;    

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {                                    #虚拟主机设置
        listen       80 default_server;                #监听端口
        listen       [::]:80 default_server;
        server_name  _;                                #访问域名
        root         /usr/share/nginx/html;       #站点根目录(存放网站程序的目录)

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {            #访问路径,可相对也可绝对路径
        }

        error_page 404 /404.html;            #错误信息返回页面
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;#错误信息返回页面
            location = /50x.html {
        }
    }
}

 

 

修改root指定路径下的index.html

步骤

[root@VM_16_8_centos html]# cd /usr/share/nginx/html

[root@VM_16_8_centos html]# mv index.html index.html.back

[root@VM_16_8_centos html]# touch index.html

[root@VM_16_8_centos html]# vim index.html

Hello World

[root@VM_16_8_centos html]# systemctl restart nginx

 

通过浏览器输入服务器的IP,查看到打印信息

 

 

总结

目前对于如何配置nginx服务还不了解,以后深入了随时回来补充。先这样咯