Nginx配置虛擬主機實戰

本文主要介紹nginx配置虛擬主機。包括基於域名(通過域名來區分虛擬主機)的虛擬主機,基於端口(通過端口來區分虛擬主機)的虛擬主機,基於ip(通過ip來區分虛擬主機)的虛擬主機。

一>Nginx模塊介紹及目錄結構

1>Nginx整體目錄結構

image.png

[root@lll nginx]# tree

├── client_body_temp       ------客戶端內容的臨時文件

├── conf                            -------nginx所有配置文件目錄(每一個配置文件都有一個默認的備份文件,最核心的配置文件是nginx.conf)

│    ├── fastcgi.conf

│    ├── fastcgi.conf.default

│    ├── fastcgi_params

│    ├── fastcgi_params.default

│    ├── koi-utf

│    ├── koi-win

│    ├── mime.types

│    ├── mime.types.default

│    ├── nginx.conf

│    ├── nginx.conf.default

│    ├── scgi_params

│    ├── scgi_params.default

│    ├── uwsgi_params

│    ├── uwsgi_params.default

│    └── win-utf

├── fastcgi_temp             -------fastcgi_temp臨時文件

├── html                          -------默認站點目錄(包括首頁文件和錯誤頁面優雅替代文件)

│    ├── 50x.html

│    └── index.html

├── logs                          --------日誌文件目錄(包括錯誤日誌和訪問日誌)

│    ├── access.log

│    ├── error.log

│    └── nginx.pid

├── proxy_temp              -------代理臨時文件

├── sbin                          -------sbin目錄下面爲nginx啓動文件

│    └── nginx

├── scgi_temp                -------scgi臨時文件

└── uwsgi_temp             -------uwsgi臨時文件

2>Nginx主配置文件nginx.conf

    Nginx的配置文件是一個純文本文件,它位於Nginx的安裝目錄的conf目錄下,整個配置文件是以塊的形式組織的。整個配置文件中Main指令位於最高層,在Main層下面有Events,Http等層級,而在http層中又包含有Server層,Server層裏面又包含有location層(並且一個Server層裏面可以有多個location層)。

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

    #    }

    #}


}


二>Nginx配置虛擬主機

A>基於域名的虛擬主機(應用外部網站)


第一步,nginx.conf修改配置:

 server {

        listen       80;

        server_name  www.etiantian.org etiantian.org ;  ---此處爲虛擬主機配置了別名


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {

            root   html/www;

            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;

        #}

    }

server {

        listen       80;

        server_name  bbs.etiantian.org;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {

            root   html/bbs;

            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;

        #}

    }

server {

        listen       80;

        server_name  blog.etiantian.org;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {

            root   html/blog;

            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;

        #}

    }

第二 步,循環創建站點目錄及首頁

[root@lll conf]# mkdir ../html/{www,blog,bbs}

[root@lll conf]# for n in www blog bbs;do echo "$n.etiantian.org" > ../html/$n/index.html;done

[root@lll conf]# for n in www blog bbs;do cat ../html/$n/index.html;done

www.etiantian.org

blog.etiantian.org

bbs.etiantian.org

第三步,檢查語法並重啓

[root@lll conf]# ../sbin/nginx -t

nginx: the configuration file /application/nginx-1.6.2/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.6.2/conf/nginx.conf test is successful

[root@lll conf]# ../sbin/nginx -s reload

第四步 在宿主機的hosts文件中給虛擬主機做解析

image.png

image.png

第五步 測試

image.png

總結(配置虛擬主機流程):

    1>複製一個完整的server標籤段到結尾(要放在http標籤裏面)。

    2>更改server_name及對應的根目錄。

    3>檢查配置文件語法,平滑重啓服務。

    4>創建server_name對應網頁的根目錄,並且建立根目錄,如果沒有index首頁會出現403。

    5>在客戶端對server_name的主機名做hosts解析或dns設置,並檢查。

    6>在客戶端瀏覽器訪問測試。


三>Nginx日誌格式介紹及切割日誌

image.png








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章