nginx+tomcat單個域名及多個域名配置

同步首發:http://www.yuanrengu.com/index.php/20171130.htmlphp

項目開發接近尾聲,開始着手在生產環境部署項目,開發階段部署項目都沒用nginx。項目是採用SOA架構,多系統開發,主要包括服務系統、中臺系統、後臺系統、金融系統、接口系統、調度系統、報表系統等。這類分佈式的系統,通常也都會用到nginx來作負載均衡。html

從公司剛成立就進來,趕鴨子上架來作架構師,負責公司的全部研發事情,搭建公司的整個技術架構,起初的全部核心業務代碼基本都由本身親自把關來進行編碼。系統也從最初的只有一個pc端,發展到現在pc中臺、後臺、android端3個app、iOS端3個app,產品越作越多,親自負責招聘面試、培訓。以前不少時候都有過無助和苦惱,由於負責公司整個架構,又要負責核心業務的編碼,技術難點的攻克,新員工的招聘及培訓,如今團隊已經都發展到16我的,並且這全是研發人員。linux

回想這一路,以爲以前看似爬不過去的山也不過如此,也許這就是成長吧,成長老是會伴隨些許汗水與淚水吧。因爲是負責團隊的全部事情,因此數據庫的維護、遷移數據、建索引等性能優化,項目部署等全部事情必須得一肩挑,不要問我爲何公司沒有DBA?爲何沒有運維?我真的只能給你一個眼神,讓你慢慢去體會。android

話很少說,直接開始技術乾貨分享。nginx

nginx作負載均衡的優點網上有不少介紹資料,這裏我再也不多作介紹。由於有不少系統要部署,涉及到域名、二級域名、多個域名等的部署。在實際的部署因爲對nginx的不夠熟悉,遇到過不少坑,其中這種多域名的配置,xxxx.com轉發到www.xxxx.com、訪問域名轉發到tomcat裏的項目等,如今先總結一部坑的解決辦法。web

如將xxxx.com這個域名指向8082端口裏的tomcat項目,在作這個介紹前先講個插曲,如訪問xxxx.com需轉向到www.xxxx.com,這一點不少人都會忽略。面試

如今若是要部署中臺、後臺、金融系統,找到nginx/conf/nginx.conf,修改配置:數據庫

 

    upstream web{
        server localhost:8082;
    }
	
	upstream admin{
        server localhost:8083;
    }
	
	upstream finance{
        server localhost:8084;
    }


	server {
        listen       80;
        server_name  finance.xxxx.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
			proxy_pass http://finance;
			proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #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;
        }
    }
	
	
    server {
        listen       80;
        server_name  www.xxx.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
			proxy_pass http://web;
			proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;			
        }

        #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 {
        server_name xxxx.com;
        rewrite ^(.*) http://www.xxxx.com$1 permanent;
    }
	
	
    server {
        listen       80;
        server_name  admin.xxxx.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
			proxy_pass  http://admin;
			proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;			
        }

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

 上面的配置還包括了訪問xxxx.com轉向www.xxxx.com的配置,以下:apache

   server {
        server_name xxxx.com;
        rewrite ^(.*) http://www.xxxx.com$1 permanent;
    }

nginx的基本配置大體就是這樣,若是綁定多個域名(無論是一級域名仍是二級域名),需配置多個server,你會發現這幾個server配置都差很少,主要是更改server_name及proxy_pass指向便可。upstream節點其實就是代理服務的訪問路徑。windows

若是此時訪問域名,你會發現nginx的配置生效了,只是目前顯示的是tomcat的默認界面。nginx的配置基本就這樣了,接下來對tomcat作些配置的修改。找到tomcat裏的conf/server.xml,註釋掉默認的Host配置,添加以下Host配置:

    <Host name="localhost" appBase="E:\tomcat\apache-tomcat-8.0.35-8082\webapps\web" deployOnStartup ="false" autoDeploy="false" unpackWARs="true">
                 <Context path="/"  docBase="E:\tomcat\apache-tomcat-8.0.35-8082\webapps\web" />
                 <Valve   className="org.apache.catalina.valves.AccessLogValve"   
                 directory="logs"     prefix="localhost_access_log"   suffix=".txt"   
                 pattern="%h %l %u %t "%r" %s %b"   />
    </Host> 

以上是windows服務器下的配置,如爲linux,只需更改appBase和docBase,指向項目的路徑。tomcat的配置也已經完成,重啓tomcat,訪問域名就指向了tomcat裏的項目。

但願能對你們有幫助,若是在使用的過程當中遇到什麼問題,能夠在底下留言。

個人博客即將搬運同步至騰訊雲+社區,邀請你們一同入駐:https://cloud.tencent.com/developer/support-plan

相關文章
相關標籤/搜索