7、Lunix下nginx反向代理伺服器域名解析配置實操

xyphf_和派孔明發表於2018-07-23

1、開啟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       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;
        #}
    
    }
    
    ##########################vhost#####################################
    include vhost/*.conf;

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

}

 

include vhost/*.conf 我們是新增了這個配置。

就是說把vhost資料夾下面的*.conf檔案載入過來,其實它的作用和我們在*.conf裡面的配置檔案配在這裡面是一樣的。

這麼做的原因是考慮以後的維護,這樣維護起來會比較方便。我們每一個域名我們都做成一個.conf檔案;

這樣我們通過主配置include進來,這樣我們以後想維護某個域名的話,我們直接去編輯對應的域名檔案就可以了。

否則這個檔案會越編輯越龐大。

 

那麼現在在我們的CentOS虛擬機器裡面來操作。

(1) 進入nginx的目錄

cd /usr/local/nginx/

(2) 進入conf

cd conf

(3) 在conf裡面建立一個資料夾,叫做vhost

sudo mkdir vhost

(4) 建立好之後,我們修改一下nginx.conf檔案

sudo vim nginx.conf

(5) 新增include vhost/*.conf

儲存退出

:wq

(6) 我們通過修改host檔案模擬一下

儲存退出

:wq

(7) 在conf下建立vhost

sudo mkdir vhost

(8) 進入vhost

cd vhost/

(9) 建立一個www.imooc.com.conf檔案

sudo vim www.imooc.com.conf
// nginx/linux_conf/vhost/learning.happymmall.com.conf


server {
    default_type 'text/html';
    charset utf-8;
    listen 80; // 表示監聽的埠是80埠
    autoindex on;
    server_name www.imooc.com; // 這裡表示線上域名
    access_log /usr/local/nginx/logs/access.log combined; // 表示nginx的 access_log
    index index.html index.htm index.jsp index.php; // 預設不輸入頁面的話預設首頁的順序
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
    }

    location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* {
        deny all;
    }

    location / {
        proxy_pass http://127.0.0.1:8080;
        add_header Access-Control-Allow-Origin *;
    }
}

這個配置的意思是監聽80埠,如果遇到www.imooc.com請求的話,那麼則轉到http://127.0.0.1:8080上;

儲存退出

:wq

現在講的是以域名轉到Http的一個請求上,稍後我們還會說一個轉發到目錄的轉發配置。

進行重啟

sudo ../../sbin/nginx -s reload

(10) 測試

這時我們訪問域名,如果開啟的是tomcat的主頁,那麼配置是正常的

這說明www.imooc.com已經轉到tomcat的8080埠上了。這就說明我們的配置已經成功的生效了。

 

下面我講另外一種轉發,nginx轉發到我們本地的資料夾上。

(1)我們在vhost資料夾下建立一個image.imooc.com.conf的資料夾

sudo vim image.imooc.com.conf
server {
    listen 80;
    autoindex off;
    server_name img.happymmall.com;
    access_log /usr/local/nginx/logs/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
    }

    location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* {
        deny all;
    }

    location / {
        root /ftpfile/;
        add_header Access-Control-Allow-Origin *;
    }
}

和剛剛講的配置最大的區別就是這個location的不一樣,其他的都是一樣的;

location是通過root關鍵字指向線上的一個資料夾/ftpfile/;

(2)重啟

cd ..
cd ..

cd sbin/

sudo ./nginx -s reload

這樣這個配置就配置好了。

 

我們訪問測試一下。

image.imooc.com/boy.jpg

這個時候圖片就訪問到了。

相關文章