centos下Nginx安装和配置多个域名的虚拟主机

nginx安装步骤,源码编译安装(源码编译,可以自定制更多功能) openssl

 

#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       80;
        server_name  localhost;

        #charset koi8-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;
        #}
    }


    # 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默认配置

 

1.解决软件正常运转所需依赖包,

yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y

2.下载源代码

wget -c https://nginx.org/download/nginx-1.12.0.tar.gz

3.解压缩

tar -zxvf nginx-1.12.0.tar.gz

4.进入源码目录,编译安装

./configure --prefix=/opt/nginx112/
make 
make install

5.进入nginx安装好的目录

cd /opt/nginx112/

6.学习nginx功能目录,nginx主目录结构如下

[root@s16ds nginx112]# ls
conf 配置文件nginx.conf(nginx的功能参数,都在这个文件定义了)
html 存放前端页面
logs 存放nginx的运行日志,错误日志
sbin 存放nginx可执行程序的目录

9.学习nginx.conf 核心配置

#nginx web核心功能在这里已定义 
http {
    #定义nginx虚拟主机的  
    server {
        #nginx监听的端口,默认浏览器是80
        listen       80;
        #填写服务器的域名,如果你有域名,nginx会解析到当前这个虚拟主机 
        #当我访问pythonav.cn:80 
        server_name  pythonav.cn;

        #location就是nginx的路径资源匹配,
        #就是当我请求
        #pythonav.cn
        #pythonav.cn/man.jpg
        #pythonav.cn/av/pian.mp4 
        #这个 location  /  这个语法是万能匹配,你所有的请求,都会进入这个location
        location / {
            #这个root参数,用于定义网页根目录,路径
            root   html;
            #定义网页的首页文件,名字且必须叫做index.html 
            index  index.html index.htm;
        }

        error_page  404              /404.html;
    }

}

 

 

10.nginx多虚拟主机

ip 和域名的关系 一对多

在自己的linux服务器上,运行2个网站

nginx.conf定义多虚拟主机配置如下:

http{
    #虚拟主机1,我门用它运行,吃鸡网站
    server{
        listen 80;
        #当我访问的域名是 s16chiji.com ,就进入这个server标签
        server_name  s16chiji.com;
            location  / {
                #返回/opt/s16chiji目录下的内容 
                root  /opt/s16chiji/;
                index index.html;
            }
    }
    #虚拟主机2,用它运行,s16韩剧网站
    server{
        listen 80;
        server_name  s16hanju.com;
        location / {
            root  /opt/s16hanju;
            index index.html;
        }
    } 
    
}

11.配置两个虚拟主机的网站资源

    1.配置吃鸡网游的资料
    在/opt/s16chiji 目录下创建index.html 
    
    2.配置韩剧网址
    在/opt/s16hanju 目录下创建index.html 
    
    
    3.配置两个本地解析的域名 ,问题?去linux下还是windows下配置??
    在本地 修改C:\Windows\System32\drivers\etc\hosts文件,写入如下配置
        192.168.15.71  s16chiji.com  
        192.168.15.71  s16hanju.com  
    
    4.在windows下测试访问 是否正常
        s16chiji.com  
        s16hanju.com  

12.定义nginx错误页面优化 404页面定制
修改nginx.conf ,找到如下参数

#通过这个参数,定义错误页面的文件 ,当状态码是 404 400 401 时,返回40x.html页面
error_page 404 401 400 403 /40x.html;

13.nginx用访问 日志access.log
找到nginx.conf开启如下功能

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;

配置填写完毕后,重启nginx,加载功能

nginx -s reload

 

posted @ 2019-02-19 12:04  Niuli'blog  阅读(1685)  评论(0编辑  收藏  举报