lnmp环境nginx 配置多虚拟主机例子

1、首先进入 /usr/local/nginx/conf/ 目录(自己nginx安装的路径) ,刚编译好的nginx 在这个目录下是木有 vhost 目录的,创建好这个目录后,打开nginx.conf 文件,在 http 范围内添加 include vhost/*.conf,包含创建的虚拟主机配置文件,然后保存,创建虚拟目录共用的server文件,就是每个conf都会使用到的配置项,我们把他独立成一个模块供大家使用。

server.conf文件:

location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass unix:/tmp/php-cgi.sock;
     fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
    {
      expires 30d;
  # access_log off;
   }
    location ~ .*\.(js|css)?$
    {
      expires 15d;
   # access_log off;
   }
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_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;#脚本文件请求的路径 
fastcgi_param  QUERY_STRING       $query_string; #请求的参数;如?app=123 
fastcgi_param  REQUEST_METHOD     $request_method; #请求的动作(GET,POST) 
fastcgi_param  CONTENT_TYPE       $content_type; #请求头中的Content-Type字段 
fastcgi_param  CONTENT_LENGTH     $content_length; #请求头中的Content-length字段。   
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name; #脚本名称  
fastcgi_param  REQUEST_URI        $request_uri; #请求的地址不带参数 
fastcgi_param  DOCUMENT_URI       $document_uri; #与$uri相同。  
fastcgi_param  DOCUMENT_ROOT      $document_root; #网站的根目录。在server配置中root指令中指定的值  
fastcgi_param  SERVER_PROTOCOL    $server_protocol; #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。   
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;#cgi 版本 
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;#nginx 版本号,可修改、隐藏 
fastcgi_param  REMOTE_ADDR        $remote_addr; #客户端IP 
fastcgi_param  REMOTE_PORT        $remote_port; #客户端端口 
fastcgi_param  SERVER_ADDR        $server_addr; #服务器IP地址 
fastcgi_param  SERVER_PORT        $server_port; #服务器端口 
fastcgi_param  SERVER_NAME        $server_name; #服务器名,域名在server配置中指定的server_name
#fastcgi_param  PATH_INFO           $path_info;#可自定义变量 
# PHP only, required if PHP was built with --enable-force-cgi-redirect 
#fastcgi_param  REDIRECT_STATUS    200;

2、准备好了公用文件server.conf 和 fastcgi.conf 后,进入vhost目录(之前手动创建的),创建虚拟主机 。
vim test.conf;

server
{
        listen 80;
        server_name test.cn;
        index index.html index.htm index.php;
        root /var/www/test;
        access_log /var/www/logs/test.log;
        error_log off;
        location / {
                try_files $uri $uri/ /index.php$uri?$args;
        }
        if (!-e $request_filename) {
                rewrite ^(.*)$ /index.php?s=$1 last;
                break;
        }
        include server.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 {
        listen       80;
        server_name   www.led.com
        index index.html index.htm index.php;
        root /usr/local/vhost/led;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root  /usr/local/vhost/led;
            index  index.html index.htm index.php;
        }

        #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;
        #}
 
    location ~ .*\.(php|php5)?$
       {
        #fastcgi_pass unix:/tmp/php-cgi.sock;
         fastcgi_pass  127.0.0.1:9000;
         fastcgi_index index.php;
         fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include fastcgi_params;
       }     

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
       {
        expires 30d;
       }

       location ~ .*\.(js|css)?$
      {
       expires 1h;
       }

    log_format access '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" $http_x_forwarded_for';
      
    }

---------------- nginx.conf

user  nginx nginx;
worker_processes  8;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        /usr/local/nginx/nginx.pid;

worker_rlimit_nofile 65535;
events {
    use epoll;
    worker_connections  65535;
}

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;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    tcp_nodelay on;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;  

    gzip  on;
    gzip_min_length 1k;
    gzip_buffers   4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types   text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

    server {

        listen 80;

        server_name _;

        server_name_in_redirect off;

        location / {

        root /usr/share/nginx/html;

        index index.html;

        }

        }

        # 包含所有的虚拟主机的配置文件

        include /usr/local/nginx/conf/vhosts/*;
     
}

时间: 2024-08-02 05:17:18

lnmp环境nginx 配置多虚拟主机例子的相关文章

Apache安装配置和虚拟主机配置教程

Apache 2.0 安装 相对IIS来说Apache建立站点更加专业,一些大型门户网站和流量大的专业站点都是通过Apache发布站点的.而且以往很多程序都是通过ASP语言完成的,ASP与IIS的兼容性比较好,那时IIS用户比较多.随着PHP语言的流行,越来越多的用户抛弃了ASP.相比IIS来说Apache与PHP的兼容性更好,执行代码的效率更高,运行起来也更加稳定. 第一步:下载Apache 2.0安装程序,并运行. 第二步:同意许可协议点"NEXT"按钮继续. 第三步:查看帮助信息

centos7-CentOS 7 怎样配置apache虚拟主机?

问题描述 CentOS 7 怎样配置apache虚拟主机? 跟ubuntu有点不同,没有看到类似的sites-enable文件夹 解决方案 CentOS下配置apache虚拟主机CentOS Apache虚拟主机配置CentOS中增加apache虚拟主机的配置

nginx配置phpmyadmin虚拟目录的问题

问题描述 nginx配置phpmyadmin虚拟目录的问题 server {listen 80; server_name ceshi3.com; #charset koi8-r; #access_log logs/host.access.log main; Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { root /www/html/c

apache虚拟主机配置-php在配置apache虚拟主机时启动虚拟主机配置后就无法启动apache

问题描述 php在配置apache虚拟主机时启动虚拟主机配置后就无法启动apache 在httpd.conf 配置虚拟主机: Virtual hosts ,虚拟主机 Include conf/extra/httpd-vhosts.conf 将上一句Include这句话打开后,apache就无法启动了,而且其他的配置都检查了,没有问题. 查看了error.log (部分log)是这样的: Starting the Apache2.2 service The Apache2.2 service is

Nginx系列教程:LNMP多用户动态进程管理虚拟主机方案

A. 特点1. 高效.内存使用少.2. 权限分离,用户间互不干扰.3. 动态进程管理,资源分配均衡. B. 应用程序说明Nginx:事件驱动的 Web 服务器,采用模块化设计,小巧.高效.PHP-FPM:支持快速进程管理的 PHP FastCGI 接口版本,用它实现动态进程管理,提高资源使用效率. C. 整个架构的简单说明Nginx 处理所有的 Web 请求,它将 PHP 的请求 Match 出,发送给上游服务器处理,这里的上游服务器就是 PHP-CGI.PHP-CGI 工作在 FastCGI

自动安装配置httpd虚拟主机的脚本

脚本练习,写一个脚本,实现本地Web服务器的配置: 1.检查httpd包是否已经安装,如果没有,则使用yum命令安装之:(需要事先配置好yum源) 2.取消中心主机,新建5个基于域名的虚拟主机,并分别提供一个测试主页,主页内容为其对应的FQDN: www.magedu.com, /vhosts/magedu bbs.magedu.com, /vhosts/bbs wp.magedu.com, /vhosts/wordpress pw.magedu.com, /vhosts/pw pma.mage

如何配置apache虚拟主机的实例小结_Linux

1.基于ip地址的虚拟主机 复制代码 代码如下: Listen 80<VirtualHost 172.20.30.40>    DocumentRoot /home/httpd/html1    ServerName www.ok1.com    ErrorLog /usr/local/apache/logs/error1_log    CustomLog /usr/local/apache/logs/access1_log combined</VirtualHost><Vi

Windows 中 Apache配置 与 虚拟主机 设置

下载Apache:http://httpd.apache.org/download.cgi 安装步骤略过.... 安装完 Apache 程序后,Monitor apache servers 无法启动,图标显示为红色停止标志.鼠标移至任务栏 Apache 服务图标显示 "No services installed",原因如下: Apache 服务没有安装成功 如果 Apache 服务程序成功安装,那么我们可以通过右键"我的电脑" ->"管理"

ubuntu 13.04 配置apache 虚拟主机方法

   代码如下 复制代码 #创建目录# $mkdir /var/www/phperstar #创建虚拟主机配置文件# $cd /etc/apache2/sites-enabled/ $vi websitename #复制一下内容,贴入配置文件,保存退出# <VirtualHost *:80>         ServerAdmin webmaster@localhost         ServerName www.111cn.net    #改成自己想用的域名#         Docume