• Nginx静态网站的部署


    静态网站的部署

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

    #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这个目录下即可,当然也可以自己定义目录,修改配置文件即可

    那么如果我们想在一个nginx上部署多个网站(不同端口),应该怎么做呢?

    配置虚拟主机

      虚拟主机,也叫“网站空间”,就是把一台运行在互联网上的物理服务器划分成多个“虚拟”服务器。虚拟主机技术极大的促进了网络技术的应用和普及。同时虚拟主机的租用服务也成了网络时代的一种新型经济形式。

    一、端口绑定(以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+port

    二、域名绑定

    我们上面部署的静态网站目前只能通过ip+port访问,那么如果想通过域名访问呢?

    答案很简单,只需要修改我们刚才添加到配置文件中的server信息中的server_name:

        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。

  • 相关阅读:
    程序集冲突问题
    Linux 菜鸟学习笔记--系统分区
    gawk
    Ansible
    Linux 网关及路由
    Linux档案与目录管理
    find命令
    sed详解
    FTP服务
    Gentoo安装(虚拟机)
  • 原文地址:https://www.cnblogs.com/blazeZzz/p/9646016.html
Copyright © 2020-2023  润新知