lnmp环境nginx 配置多虚拟主机例子
配置多虚拟主机是每一台WEB服务器几乎会做的一个动作了,特别是我们这种小站长了,下文小编来为各位介绍lnmp环境nginx 配置多虚拟主机例子,希望例子可以帮助到大家.
1、首先进入 /usr/local/nginx/conf/ 目录(自己nginx安装的路径),刚编译好的nginx 在这个目录下是木有 vhost 目录的,创建好这个目录后,打开nginx.conf 文件,在 http 范围内添加 include vhost/*.conf,包含创建的虚拟主机配置文件,然后保存,创建虚拟目录共用的server文件,就是每个conf都会使用到的配置项,我们把他独立成一个模块供大家使用.
server.conf文件:
- location~.*\.(php|php5)?$
- {
- #fastcgi_passunix:/tmp/php-cgi.sock;
- fastcgi_pass127.0.0.1:9000;
- fastcgi_indexindex.php;
- includefastcgi.conf;
- }
- location~.*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
- {
- expires30d;
- #access_logoff;
- }
- location~.*\.(js|css)?$
- {
- expires15d;
- #access_logoff;
- }
- fastcgi_index:(nginx的默认首页文件)
如果URI以斜线结尾,文件名将追加到URI后面,这个值将存储在变量$fastcgi_script_name中.
例如:
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
fastcgi_pass:指定FastCGI服务器监听端口与地址,可以是本机或者其它.
用netstat -tln 查看端口使用情况:
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN
可以看到9000端口处于监听状态:另外的其他fastcgi配置,放入fastcgi.conf公用配置文件中,server.conf 来包含他.
fastcgi.conf文件相关配置项:
- ?fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;#脚本文件请求的路径
- fastcgi_paramQUERY_STRING$query_string;#请求的参数;如?app=123
- fastcgi_paramREQUEST_METHOD$request_method;#请求的动作(GET,POST)
- fastcgi_paramCONTENT_TYPE$content_type;#请求头中的Content-Type字段
- fastcgi_paramCONTENT_LENGTH$content_length;#请求头中的Content-length字段。
- fastcgi_paramSCRIPT_NAME$fastcgi_script_name;#脚本名称
- fastcgi_paramREQUEST_URI$request_uri;#请求的地址不带参数
- fastcgi_paramDOCUMENT_URI$document_uri;#与$uri相同。
- fastcgi_paramDOCUMENT_ROOT$document_root;#网站的根目录。在server配置中root指令中指定的值
- fastcgi_paramSERVER_PROTOCOL$server_protocol;#请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
- fastcgi_paramGATEWAY_INTERFACECGI/1.1;#cgi版本
- fastcgi_paramSERVER_SOFTWAREnginx/$nginx_version;#nginx版本号,可修改、隐藏
- fastcgi_paramREMOTE_ADDR$remote_addr;#客户端IP
- fastcgi_paramREMOTE_PORT$remote_port;#客户端端口
- fastcgi_paramSERVER_ADDR$server_addr;#服务器IP地址
- fastcgi_paramSERVER_PORT$server_port;#服务器端口
- fastcgi_paramSERVER_NAME$server_name;#服务器名,域名在server配置中指定的server_name
- #fastcgi_paramPATH_INFO$path_info;#可自定义变量
- #PHPonly,requiredifPHPwasbuiltwith--enable-force-cgi-redirect
- #fastcgi_paramREDIRECT_STATUS200;
2、准备好了公用文件server.conf 和 fastcgi.conf 后,进入vhost目录,之前手动创建的,创建虚拟主机.
- vimtest.conf;
- server
- {
- listen80;
- server_nametest.cn;
- indexindex.htmlindex.htmindex.php;
- root/var/www/test;
- access_log/var/www/logs/test.log;
- error_logoff;
- location/{
- try_files$uri$uri//index.php$uri?$args;
- }
- if(!-e$request_filename){
- rewrite^(.*)$/index.php?s=$1last;
- break;
- }
- includeserver.conf;
- }
test.conf 虚拟主机文件配置完成
重启nginx:
记得先验证测试后再重启,否则会出现nginx 重启不来.
测试:/usr/local/nginx/sbin/nginx -t
没问题后进行重启:
重启:/usr/local/nginx/sbin/nginx -s reload
如果没有域名来解析指定到自己的主机ip,可以直接把自己的主机ip 指到本地,编辑C:\Windows\System32\drivers\etc\hosts 文件就可,加入:
- <二、我的实例>
- ---------vhosts/led.conf
- server{
- listen80;
- server_namewww.led.com
- indexindex.htmlindex.htmindex.php;
- root/usr/local/vhost/led;
- #charsetkoi8-r;
- #access_loglogs/host.access.logmain;
- location/{
- root/usr/local/vhost/led;
- indexindex.htmlindex.htmindex.php;
- }
- #error_page404/404.html;
- #redirectservererrorpagestothestaticpage/50x.html
- #
- error_page500502503504/50x.html;
- location=/50x.html{
- roothtml;
- }
- #proxythePHPscriptstoApachelisteningon127.0.0.1:80
- #
- #location~\.php${
- #proxy_passhttp://127.0.0.1;
- #}
- location~.*\.(php|php5)?$
- {
- #fastcgi_passunix:/tmp/php-cgi.sock;
- fastcgi_pass127.0.0.1:9000;
- fastcgi_indexindex.php;
- fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
- includefastcgi_params;
- }
- location~.*\.(gif|jpg|jpeg|png|bmp|swf)$
- {
- expires30d;
- }
- location~.*\.(js|css)?$
- {
- expires1h;
- }
- log_formataccess'$remote_addr-$remote_user[$time_local]"$request"'
- '$status$body_bytes_sent"$http_referer"'
- '"$http_user_agent"$http_x_forwarded_for';
- }
- ----------------nginx.conf
- usernginxnginx;
- worker_processes8;
- error_loglogs/error.log;
- #error_loglogs/error.lognotice;
- #error_loglogs/error.loginfo;
- pid/usr/local/nginx/nginx.pid;
- worker_rlimit_nofile65535;
- events{
- useepoll;
- worker_connections65535;
- }
- http{
- includemime.types;
- default_typeapplication/octet-stream;
- #log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
- #'$status$body_bytes_sent"$http_referer"'
- #'"$http_user_agent""$http_x_forwarded_for"';
- #access_loglogs/access.logmain;
- server_names_hash_bucket_size128;
- client_header_buffer_size32k;
- large_client_header_buffers432k;
- client_max_body_size8m;
- sendfileon;
- tcp_nopushon;
- #keepalive_timeout0;
- keepalive_timeout65;
- tcp_nodelayon;
- fastcgi_connect_timeout300;
- fastcgi_send_timeout300;
- fastcgi_read_timeout300;
- fastcgi_buffer_size64k;
- fastcgi_buffers464k;
- fastcgi_busy_buffers_size128k;
- fastcgi_temp_file_write_size128k;
- gzipon;//phpfensi.com
- gzip_min_length1k;
- gzip_buffers416k;
- gzip_http_version1.0;
- gzip_comp_level2;
- gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;
- gzip_varyon;
- server{
- listen80;
- server_name_;
- server_name_in_redirectoff;
- location/{
- root/usr/share/nginx/html;
- indexindex.html;
- }
- }
- #包含所有的虚拟主机的配置文件
- include/usr/local/nginx/conf/vhosts/*;
- }
热门评论