nginx基本域名

基於域名虛擬主機

[root@localhost nginx]# grep html conf/nginx.conf

            root   html;          #docmont  根目錄

            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;

        #    root           html;

    #        root   html;

    #        index  index.html index.htm;

    #        root   html;

    #        index  index.html index.htm;


[root@localhost html]# vim index.html



基本於域名的虛擬主機配置

[root@localhost nginx]# tree

.

|-- client_body_temp

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

|-- html           #默認站點目錄,相當於apache htdocs目錄

|   |-- 50x.html     #錯誤頁面

|   `-- index.html  #首頁文件

|-- logs   #日誌目錄

|   |-- access.log

|   |-- error.log

|   `-- nginx.pid

|-- proxy_temp

|-- sbin     #啓動目錄

|   `-- nginx

|-- scgi_temp

`-- uwsgi_temp


[root@localhost conf]# egrep -v "#|^$" nginx.conf

worker_processes 1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {                             #相當於apache vhost

        listen       80;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

}

[root@localhost conf]# egrep -v "#|^$" nginx.conf >nginx.conf.tmp

[root@localhost conf]# mv nginx.conf.tmp nginx.conf


[root@localhost conf]# cat fastcgi.conf


fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

fastcgi_param  QUERY_STRING       $query_string;

fastcgi_param  REQUEST_METHOD     $request_method;

fastcgi_param  CONTENT_TYPE       $content_type;

fastcgi_param  CONTENT_LENGTH     $content_length;


fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

fastcgi_param  REQUEST_URI        $request_uri;

fastcgi_param  DOCUMENT_URI       $document_uri;

fastcgi_param  DOCUMENT_ROOT      $document_root;

fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  HTTPS              $https if_not_empty;


fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;

fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;


fastcgi_param  REMOTE_ADDR        $remote_addr;

fastcgi_param  REMOTE_PORT        $remote_port;

fastcgi_param  SERVER_ADDR        $server_addr;

fastcgi_param  SERVER_PORT        $server_port;

fastcgi_param  SERVER_NAME        $server_name;


# PHP only, required if PHP was built with --enable-force-cgi-redirect

fastcgi_param  REDIRECT_STATUS    200


[root@localhost nginx]# vim conf/nginx.conf

user nginx nginx;  #nginx用戶名和組

worker_processes  8;   #進程數可以是CPU的數量*2

events {

    use epoll;          #事件

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  www.you.com you.com;

        location / {

            root   /data0/www/www;

            index  index.html index.htm;

        }

#       error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

}


[root@localhost conf]# mkdir /data0/www/{www,bbs,blog} -p

[root@localhost conf]# tree /data0/

/data0/

`-- www

    |-- bbs

    |-- blog

    `-- www


4 directories, 0 files

[root@localhost conf]# chown -R nginx.nginx /data0/www


[root@localhost conf]# chown -R nginx.nginx /data0/www


[root@localhost nginx]# ./sbin/nginx -s reload    #平滑啓起


基本於多個域名

[root@localhost nginx]# vim conf/nginx.conf

user nginx nginx;

worker_processes  8;

events {

    use epoll;

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  www.you.com you.com;

        location / {

            root   /data0/www/www;

            index  index.html index.htm;

        }

#       error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

######

server {

        listen       80;

        server_name  www.bbs.com bbs.com;

        location / {

            root   /data0/www/bbs;

            index  index.html index.htm;

        }

#       error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

######

server {

        listen       80;

        server_name  www.blog.com blog.com;

        location / {

            root   /data0/www/blog;

            index  index.html index.htm;

        }

#       error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }


}


[root@localhost nginx]# ./sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful


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


加日誌

[root@localhost nginx]# mkdir -p /app/logs

[root@localhost nginx]# vim conf/nginx.conf 

user nginx nginx;

worker_processes  8;

error_log  /app/logs/nginx_error.log  crit;

pid        logs/nginx.pid;       #加錯誤日誌


events {

    use epoll;

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    log_format  commonlog  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';    #正確日誌

    sendfile        on;  

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  www.you.com you.com;

        location / {

            root   /data0/www/www;

            index  index.html index.htm;

            access_log /app/logs/www_access.log  commonlog;

        }

    }

######

server {

        listen       80;

        server_name  www.bbs.com bbs.com;

        location / {

            root   /data0/www/bbs;

            index  index.html index.htm;

            access_log /app/logs/bbs_access.log  commonlog;

        }

    }

######

server {

        listen       80;

        server_name  www.blog.com blog.com;

        location / {

            root   /data0/www/blog;

            index  index.html index.html;

            access_log /app/logs/blog_access.log  commonlog;

        }

    }


}


service 和配置文件分開


[root@localhost nginx]# mkdir extra

[root@localhost nginx]# cp conf/nginx.conf   extra/nginx_vhosts.conf


主配置文件

[root@localhost nginx]# cat conf/nginx.conf

user nginx nginx;

worker_processes  8;

error_log  /app/logs/nginx_error.log  crit;

pid        logs/nginx.pid;


events {

    use epoll;

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    log_format  commonlog  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile        on;

    keepalive_timeout  65;

    include /usr/local/nginx/extra/nginx_vhosts.conf;      #包含虛擬主機的文件,可以包含多個虛擬主機文件

}



虛擬主機配置

[root@localhost nginx]# cat extra/nginx_vhosts.conf 

    server {

        listen       80;

        server_name  www.you.com you.com;

        location / {

            root   /data0/www/www;

            index  index.html index.htm;

            access_log /app/logs/www_access.log  commonlog;

        }

    }

######

server {

        listen       80;

        server_name  www.bbs.com bbs.com;

        location / {

            root   /data0/www/bbs;

            index  index.html index.htm;

            access_log /app/logs/bbs_access.log  commonlog;

        }

    }

######

server {

        listen       80;

        server_name  www.blog.com blog.com;

        location / {

            root   /data0/www/blog;

            index  index.html index.html;

            access_log /app/logs/blog_access.log  commonlog;

        }

    }


[root@localhost nginx]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -s reload


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