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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM