Nginx安装好了,可以再html文件夹中放置一些静态资源进行访问测试。但我们都知道Nginx最大的特点就是方向代理和负载均衡,所以如果想要弄懂这些,我们还需要将其配置文件弄懂才可以进行更深入的操作。下面我满来看看Nginx配置:

[root@NFS ~]# cd /application/nginx/conf/
[root@NFS conf]# ll
total 60
-rw-r--r-- 1 root root 1077 Nov 26 16:57 fastcgi.conf
-rw-r--r-- 1 root root 1077 Nov 26 16:57 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 Nov 26 16:57 fastcgi_params
-rw-r--r-- 1 root root 1007 Nov 26 16:57 fastcgi_params.default
-rw-r--r-- 1 root root 2837 Nov 26 16:57 koi-utf
-rw-r--r-- 1 root root 2223 Nov 26 16:57 koi-win
-rw-r--r-- 1 root root 3957 Nov 26 16:57 mime.types
-rw-r--r-- 1 root root 3957 Nov 26 16:57 mime.types.default
-rw-r--r-- 1 root root 2656 Nov 26 16:57 nginx.conf
-rw-r--r-- 1 root root 2656 Nov 26 16:57 nginx.conf.default
-rw-r--r-- 1 root root  636 Nov 26 16:57 scgi_params
-rw-r--r-- 1 root root  636 Nov 26 16:57 scgi_params.default
-rw-r--r-- 1 root root  664 Nov 26 16:57 uwsgi_params
-rw-r--r-- 1 root root  664 Nov 26 16:57 uwsgi_params.default
-rw-r--r-- 1 root root 3610 Nov 26 16:57 win-utf
[root@NFS conf]# 

conf文件夹中是nginx的所有配置,主配置文件为nginx.conf。其中带有".default"后缀的文件是备份文件,比较如下:

[root@NFS conf]# vimdiff nginx.conf nginx.conf.default 
2 files to edit

+ +--117 lines: user  nobody;---------------------------------------------|+ +--117 lines: user  nobody;---------------------------------------------
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
  ~                                                                       |  ~                                                                       
nginx.conf                                              1,0-1          All nginx.conf.default                                      1,0-1          All
"nginx.conf.default" 117L, 2656C

 

将注释清除,精简配置说明如下:

[root@NFS conf]# grep -Ev "#|^$" nginx.conf.default >nginx.conf  #去掉包含#和空行的内容
[root@NFS conf]# vim nginx.conf

worker_processes  1;                                             #worker进程的数量
events {                                                         #event区块
    worker_connections  1024;                                    #每个worker进程支持的最大连接数
}
http {                                                           #http区块
    include       mime.types;                                    #Nginx支持的媒体类型库文件
    default_type  application/octet-stream;                      #默认媒体类型
    sendfile        on;                                          #开启高效传输模式
    keepalive_timeout  65;                                       #连接超时,超过65秒自动断连
    server {                                                     #server区块(第一个,可以有多个)
        listen       80;                                         #提供服务的端口,默认80
        server_name  localhost;                                  #提供服务的域名主机名
        location / {                                             #第一个location区块(可以有多个)
            root   html;                                         #站点根目录
            index  index.html index.htm;                         #默认的首页文件,多个用空格分开
        }
        error_page   500 502 503 504  /50x.html;                 #出现对应的http状态码,使用50x.html回应客户端
        location = /50x.html {                                   #location区块(第二个)
            root   html;                                         #指定对应的站点目录为html
        }
    }
}

nginx配置文件组成:

  • main     nginx主区块
  • event    nginx事件区块
  • http     nginx http 功能区块
  • server    nginx网站主机区块
  • location  nginx匹配或者定位区块

 

nginx配置语法说明:

  • 大括号要成对出现
  • 每一行指令后面要用分号结尾
  • 每一个指令要放置在指定的区块中

 

实例1:新建文件夹www,并将www.daydayup.com 执行该文件夹。

  1. 在本地电脑解析域名 www.daydayup.com,修改 C:\Windows\System32\drivers\etc\hosts。

     
  2. 检查域名解析是否成功。
    [e:\~]$ ping www.daydayup.com
    
    正在 Ping www.daydayup.com [10.0.0.31] 具有 32 字节的数据:
    来自 10.0.0.31 的回复: 字节=32 时间<1ms TTL=64
    来自 10.0.0.31 的回复: 字节=32 时间<1ms TTL=64
    来自 10.0.0.31 的回复: 字节=32 时间<1ms TTL=64
    
    10.0.0.31 的 Ping 统计信息:
        数据包: 已发送 = 3,已接收 = 3,丢失 = 0 (0% 丢失),
    往返行程的估计时间(以毫秒为单位):
        最短 = 0ms,最长 = 0ms,平均 = 0ms
  3. 创建 html/www 文件夹与 html/www/index.html 文件
    [root@NFS html]# mkdir www
    [root@NFS html]# echo "I am Cool ....">www/index.html
    [root@NFS html]# cat www/index.html 
    I am Cool ....
    [root@NFS html]#

     
  4. 编辑虚拟机10.0.0.31中nginx的配置文件nginx.conf。
    [root@NFS conf]# vim nginx.conf
    
    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  www.daydayup.com;                 #绑定www.daydayup.com域名
            location / {
                root   html/www;                           #在html中新建文件夹www
                index  index.html index.htm;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }

     

  5. 启动nginx。

    [root@NFS nginx]# sbin/nginx          #启动nginx服务
    [root@NFS nginx]# ps -ef|grep nginx   #查看nginx进程
    root       7351      1  0 19:45 ?        00:00:00 nginx: master process sbin/nginx
    www        7352   7351  0 19:45 ?        00:00:00 nginx: worker process
    root       7455   1446  0 20:11 pts/0    00:00:00 grep --color=auto nginx
    [root@NFS nginx]# 

     

  6. 在本地电脑访浏览器上问 www.daydayup.com。

 

最后修改于 2019-12-02 10:42:44
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇