当前位置:C++技术网 > 精选软件 > 云平台开发架构分析系列:9 Nginx服务器默认配置文件入门分析1

云平台开发架构分析系列:9 Nginx服务器默认配置文件入门分析1

更新时间:2017-06-25 14:21:49浏览次数:1+次

        在文章《云平台开发架构分析系列7:Nginx服务器环境安装搭建》我们已经安装好了Nginx服务器。在文章《云平台开发架构分析系列8:Nginx服务器初次使用讲解》对刚安装完的Nginx的相关文件进行了介绍,从而让你有一个整体的认识。

        而nginx配置文件则是关键。安装完nginx后,会自动生成一个默认配置文件,nginx.conf这个文件是正在起作用的文件,nginx.conf.default则是一个模板。如果前面这个文件被你改坏了,可以复制后面的文件,去掉后面.default就可以了。

        nginx配置文件的相关配置项,是玩转nginx必须熟悉的。所以我们这篇文章就默认的配置做一个简单的说明,让我们知道默认配置好的静态网站都是由哪些配置决定的。我们这里只是做一个入门级别的介绍,不会讲的太多太深入的东西,不会看不懂的。

        在讲解配置项之前,我们先来了解一下配置文件如何起作用的。

        nginx服务器的配置文件,只会在nginx主进程启动时才会加载到内存。也就是说,对于配置文件的修改,对于正在运行的nginx程序是不起作用的。如果我们要让配置起作用,就要重启nginx来加载配置文件。所以不要觉得修改之后为什么配置没有起作用,记得要重启nginx。

        启动和关闭nginx在之前已经针对进程给出了最原始的方法,那就是找到进程的文件和PID,然后启动和干掉。当然这个是可以的。然而如果要优雅的操作nginx,这样显得有点简单粗暴,对nginx也是不友好的。友好的方式应该是,“hey,nginx,请你重启一下,并加载配置。”。这样的方式显得更加人性化。

        nginx程序并不只是可以直接启动,还可以带参数执行。下面是一组操作命令:

    1.启动nginx

    ./nginx   【直接启动程序就行,如果在当前目录下,加上./,如果不是,则使用绝对路径。下同】

    2.停止nginx

    ./nginx -s stop  【快速停止nginx】
./nginx -s quit   【完整有序的停止nginx】
3.重新启动(重启nginx)
./nginx -s reload  【修改配置后重新加载生效】
./nginx -s reopen  【重新打开日志文件】

    4.如果修改了配置文件,可以测试配置文件是否正确

    ./nginx -t -c /usr/local/nginx/conf/nginx.conf  【测试nginx配置文件是否正确,不管nginx是否在执行都可以测试】

    

        这样,我们就不用每次去找nginx的进程PID,然后用最原始的方式关闭nginx,然后再重启了。当我们修改了配置文件时,我们只需要带参数的执行一下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;
    #    }
    #}
}

    

        对于一行中以#号开头的内容,我们可以先不看,因为#号表示注释。注释后的一行,内容是不生效的。为了简化说明,我们先将注释的内容都删除掉,看看默认起作用的有哪些配置。简化后如下:

    

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 index.htm;
        }
        #error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
     简化后,去掉大量的注释内容,一下子就清楚了很多。我们访问一个网站,会请求这个网站的主页。主页是一个网页文件。而想正确的访问到这个网页的内容,客户端会建立TCP连接,向服务器发起http请求。服务器这边必然需要先监听一个端口,才能响应客户端发过来的请求。所以我们要配置一个监听端口。这就是看到的:

    

    listen 80;

        我们运行起来的一个网站,是一个虚拟主机。虚拟主机的意思就是,它并不是一台真实的主机,而是通过程序模拟出来的。也就是说,nginx可以模拟很多个虚拟主机,可以同时配置运行很多个网站。所以针对虚拟主机,nginx使用server来归类,所以对虚拟主机的配置都放在这里面。所以你可以看到,server后面的花括号内放了一组配置,其中就包括了这个虚拟主机监听的端口号。

        而这个虚拟主机实际上是处理http请求而存在的,所以,server是http的一个子集。一个http可以配置很多的server虚拟主机。所以server是写在http的花括号内的。如果要在http服务器(nginx服务器的其中一种功能)里配置多个server虚拟主机,那么这些主机如何区分呢?

        区分的不仅有监听的端口,还有后面的server_name。server_name是主机名的意思,也就是主机的域名。我们在这里添加一个域名,然后将域名解析到这个服务器的公网IP,这样我们就可以用域名访问我们配置的这个网站了。

        多个虚拟主机可以使用同一个域名,也可以使用同一个监听端口。多个虚拟主机使用域名+端口的方式唯一确定一个虚拟主机。所以我们可以配置很多个都是80端口的虚拟主机(网站),只要域名不同就行了。我们在网上买的虚拟主机,也就是这个道理。大家都是购买了其中一个虚拟主机,都是80端口,每一个人的IP和域名不一样而已。而如果你自己可以配置,你只有一个域名,这样的话,你可以使用多个端口来共用一个域名,也是可以的。如果端口是80,那么在访问时可以省略端口。如果不是80,那么域名后面需要带上端口号,如http:www.cjjjs.com:8000.

        了解这两个基本配置,我们对虚拟主机(网站)有了一定的认识了。但是对于新手来说,可能还是感觉信息量有点大,所以本篇就讲到这,请先消化一下。下一篇继续往后讲解。

        下面是支持两个域名都是用80端口的两个虚拟主机的配置文件:

    

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   /data/html;
            index  index.html index.htm;
        }
        error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /data/html;
        }
    }

    server {
        listen       80;
        server_name  hzy.w3tong.com;
        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;
        }
    }
}
     你可以自行修改listen和server_name进行组合哦。