首页 > 代码库 > nginx基本域名

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


nginx基本域名