Nginx虚拟主机配置教程

20 篇文章 0 订阅

Nginx的主配置文件是:nginx.conf

nginx.conf 主要组成如下:


server可以有多个,即多个虚拟主机:

如下所示为 nginx.conf 的配置方法:


worker_processes  auto;

error_log  "E:/www/nginx/logs/error.log";

pid        "E:/www/nginx/logs/nginx.pid";


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    client_body_temp_path  "E:/www/nginx/tmp/client_body" 1 2;
    proxy_temp_path "E:/www/nginx/tmp/proxy" 1 2;
    fastcgi_temp_path "E:/www/nginx/tmp/fastcgi" 1 2;
    scgi_temp_path "E:/www/nginx/tmp/scgi" 1 2;
    uwsgi_temp_path "E:/www/nginx/tmp/uwsgi" 1 2;

    access_log  "E:/www/nginx/logs/access.log";

    sendfile        on;

    keepalive_timeout  65;

    gzip on;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_vary on;
    gzip_types text/plain
               text/xml
               text/css
               text/javascript
               application/json
               application/javascript
               application/x-javascript
               application/ecmascript
               application/xml
               application/rss+xml
               application/atom+xml
               application/rdf+xml
               application/xml+rss
               application/xhtml+xml
               application/x-font-ttf
               application/x-font-opentype
               application/vnd.ms-fontobject
               image/svg+xml
               image/x-icon
               application/atom_xml;

    gzip_buffers 16 8k;

    add_header X-Frame-Options SAMEORIGIN;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    #多个虚拟主机 server
    server {
    listen       80;  #端口
    #应用名称
    server_name  pubstage.cheyun.com test.pubstage.cheyun.com;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        #服务器代码路径
        root   E:/www/nginx/html/pubstage.cheyun.com;
        #默认访问文件
        index  index.php 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           E:/www/nginx/html/pubstage.cheyun.com;
        fastcgi_read_timeout 300;
        
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
#第二个虚拟主机:
server {
    #根据端口的不同创建不同的虚拟主机
    listen       8080;
    server_name  phpmyadmin;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   E:/www/nginx/html/phpmyadmin;
        index  index.php 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           E:/www/nginx/html/phpmyadmin;
        fastcgi_read_timeout 300;
        
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

}
第二步修改Windows系统的hosts文件:
C:\Windows\System32\drivers\etc\hosts


127.0.0.1     localhost
127.0.0.1     pubstage.cheyun.com
127.0.0.1     phpmyadmin

此时配置完成


访问路径  http://pubstage.cheyun.com   即为访问 E:/www/nginx/html/pubstage.cheyun.com  中的代码


访问  http://phpmyadmin:8080  即为访问  E:/www/nginx/html/phpmyadmin  中的代码









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值