Nginx靜態網站的部署

靜態網站的部署

首先先看一下nginx/conf/nginx.conf 配置文件內的信息:php

#user  nobody;
worker_processes  1;

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

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
     
    #gzip  on;

    #下面這個server就是默認的主機信息
    #listen 端口, server_name 主機名稱, location-root 網站根目錄,location-index 網站默認頁面
    server {
        listen       80;
        server_name  localhost;

        #charset koi9-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            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;
        }

        # 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           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    
    #咱們之後本身配置虛擬主機的話 就在這個地方新添加server便可

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
nginx.conf

因此咱們部署靜態網站,把靜態的html頁面相關放到html這個目錄下便可,固然也能夠本身定義目錄,修改配置文件便可html

那麼若是咱們想在一個nginx上部署多個網站(不一樣端口),應該怎麼作呢?nginx

 

配置虛擬主機

  虛擬主機,也叫「網站空間」,就是把一臺運行在互聯網上的物理服務器劃分紅多個「虛擬」服務器。虛擬主機技術極大的促進了網絡技術的應用和普及。同時虛擬主機的租用服務也成了網絡時代的一種新型經濟形式。web

1、端口綁定(以web1,web2兩個網站爲例)

1,將網站web1(web1網站的全部靜態文件,好比web001.html),web2,分別上傳到/usr/local/nginx/web1和/usr/local/nginx/web2下瀏覽器

2,修改nginx/conf/nginx.conf 配置文件:(添加兩個server信息)服務器

    server {
        listen       81;
        server_name  localhost;
        location / {
            root   web1;
            index  web001.html;
        }      
    }
    server {
        listen       82;
        server_name  localhost;
        location / {
            root   web2;
            index  web002.html;
        }        
    }

3,重啓nginx網絡

./nginx -s reload

4,用瀏覽器訪問ip+portsession

 

2、域名綁定

咱們上面部署的靜態網站目前只能經過ip+port訪問,那麼若是想經過域名訪問呢?app

答案很簡單,只須要修改咱們剛纔添加到配置文件中的server信息中的server_name:tcp

    server {
        listen       80;
        server_name  web1.zy.com;
        location / {
            root   web1;
            index  web001.html;
        }      
    }
    server {
        listen       80;
        server_name  web2.zy.com;
        location / {
            root   web2;
            index  web002.html;
        }        
    }

前提是你須要有這個域名,並將這個域名指向了這個ip。

相關文章
相關標籤/搜索