Nginx 入门系列二之利用 Nginx 搭建一个静态资源 Web 服务器

1 启动 Nginx

打开命令行,输入以下命令,从而启动 Nginx
在这里插入图片描述
然后我们测试一下,打开 http://localhost:80,发现出现以下页面,说明我们启动 Nginx 成功。
在这里插入图片描述

2 配置文件 nginx.conf

nginx.conf 是 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 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;
    #    }
    #}

}

尽管上面文件内容很多,事实上可以把它分成几个主要模块,我们分层如下:

...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}

其中每个模块功能如下:

  1. 全局块:配置影响 nginx 全局的指令。一般有运行 nginx 服务器的用户组,nginx 进程 pid
    存放路径,日志存放路径,配置文件引入,允许生成 worker process 数等。
  2. events 块:配置影响 nginx 服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
  3. http 块:可以嵌套多个 server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type 定义,日志自定义,是否使用 sendfile 传输文件,连接超时时间,单连接请求数等。
  4. server 块:配置虚拟主机的相关参数,一个 http 中可以有多个 server。
  5. location 块:配置请求的路由,以及各种页面的处理情况。

3 搭建一个静态资源 Web 服务器

下面我们尝试使用 Nginx 搭建一个静态资源 Web 服务器,具体实现为把本机的80端口的访问请求转到我们自己的网站主页上。

首先先添加一个页面,我在 nginx 的解压目录下新建了一个文件夹 file,里面存放了一个测试页面

在这里插入图片描述
其内容如下,很简单的一个页面:
在这里插入图片描述
好的,我们需要修改一下配置文件, location 块上的内容:

		location / {
            root   html;
            index  index.html index.htm;
        }

location /表示匹配所有请求,root html 代表使用相对路径,到 nginx 的解压目录下的 html 文件夹去寻找文件,index index.html index.htm指定默认的访问页面,现在我们对其稍作修改,修改结果如下:

		location / {
            alias   file/;
            index   test.html;
            #root   html;
            #index  index.html index.htm;
        }

alias 与 root 的作用大致相同,但区别在于:

  1. 最主要的区别在于 nginx 如何解释 location 后面的 uri,使得两条指令会以不同的方式将请求映射到服务器文件上,其中 root 指令的处理结果为 root 路径 + location 路径,而 alias 指令的处理结果为使用 alias 路径替换 location 路径。
  2. alias 是一个目录别名的定义,root 则是最上层目录的定义。

现在我们的配置文件已经修改完毕,接下来我们应该令 nignx 重新加载配置文件,在命令行输入以下命令

nginx -s reload

命令执行成功后,我们再次访问 localhost:80,发现其已经可以跳转到我们的测试页面中。
在这里插入图片描述
这样,我们的一个简单的静态资源 Web 服务器就使用 Nginx 搭建成功了。

4 关闭 Nginx

有两种关闭 Nginx 的方法。

nginx -s quit

从容停止服务,较为温和,在进程完成当前工作后再停止

nginx -s stop

无论进程是否在工作,都直接停止进程。

参考:Nginx配置详解

发布了127 篇原创文章 · 获赞 237 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Geffin/article/details/103110219