1. 程式人生 > >nginx+tomcat反向代理,及http2使用

nginx+tomcat反向代理,及http2使用

一、下載安裝

下載安裝參考連結:

2、tar -xvzf nginx-1.13.7.tar.g 解壓

3、Nginx的安裝依賴於以下三個包,意思就是在安裝Nginx之前首先必須安裝一下的三個包,安裝順序為我寫的順序:

下載後放入伺服器解壓。

4、進入nginx資料夾執行命令:./configure --with-openssl=/usr/java/openssl--with-zlib=/usr/java/zlib --with-pcre=/usr/java/pcre --with-http_v2_module--with-http_ssl_module --with-http_mp4_module --with-ipv6 --with-stream  --with-stream_ssl_module --with-file-aio--with-threads --with-debug

後面幾個with可以視情況不要

5、執行命令;make

       若報錯:error: You need a C++ compiler for C++ support

       執行命令:yum install -y gcc gcc-c++

6、執行命令:make install

7、檢查安裝是否成功

執行命令1:cd /usr/local/nginx/sbin/

執行命令2:./nginx -t

顯示如下圖表明成功。

8、可以把/usr/local/nginx下面的sbin複製到自定義的安裝目錄(sbin下啟動指令碼)

但是日誌、快取等一些檔案還是在/usr/local/nginx目錄下(這裡就體現部分坑了)

自定義目錄啟動會遇到許可權問題:Permissiondenied,執行命令:chmod -R 777 sbin/nginx

//啟動命令--安裝路徑下的/nginx/sbin/nginx
//停止命令--路徑下的/nginx/sbin/nginx -s stop或者 : nginx -s quit
//重啟命令--路徑下的/nginx/sbin/nginx -s reload
//檢視程序命令--ps -ef | grep nginx
//平滑重啟--ll -HUP Nginx主程序號/
檢視nginx啟動狀況命令:ps aux | grep nginx

9、執行命令:./nginx #啟動直到沒有報錯,才算啟動完成

10、nginx

預設埠為80,開啟埠許可權,避免埠衝突。

二、反向代理及http2配置

1、修改tomcat server.xml檔案(除埠號其它用server.xml預設的就行)

<Connector port="8088"maxThreads="150" minSpareThreads="25"connectionTimeout="20000" enableLookups="false"maxHttpHeaderSize="8192" protocol="HTTP/1.1"                      useBodyEncodingForURI="true"redirectPort="8443" acceptCount="100"disableUploadTimeout="true"

                            proxyName="aurorascm.de"proxyPort="443" scheme="https" secure="true"/>

2、配置nginx.cnf檔案

nginx https 反向代理 tomcat的二種方法參考連結:http://blog.51yip.com/apachenginx/1877.html

下面程式碼只有ngnix配置了ssl,tomcat並沒有配置,如果tomcat也配置了ssl,那麼proxy_pass  http://0.0.0.0:8080要變為

proxy_pass  https://0.0.0.0:8080

#執行使用者 
#user  nobody;
#啟動程序,通常設定成和cpu核心的數量相等
worker_processes  8;

#全域性錯誤日誌及PID檔案
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

#工作模式及連線數上限
events {
	#epoll是多路複用IO(I/O Multiplexing)中的一種方式,  
    #僅用於linux2.6以上核心,可以大大提高nginx的效能
	use   epoll;
	#單個後臺worker process程序的最大併發連結數
    worker_connections  1024;
	
	# 併發總數是 worker_processes 和 worker_connections 的乘積(一般根據經驗除以4)  
    # 即 max_clients = worker_processes * worker_connections/4
}


http {
    include       mime.types;
    default_type  application/octet-stream;
	charset utf-8;
	#設定日誌格式
    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 指令指定 nginx 是否呼叫 sendfile 函式(zero copy 方式)來輸出檔案,  
    #對於普通應用,必須設為 on,  
    #如果用來進行下載等應用磁碟IO重負載應用,可設定為 off,  
    #以平衡磁碟與網路I/O處理速度,降低系統的uptime.
    sendfile        on;
    #tcp_nopush     on;

	#連線超時時間
    #keepalive_timeout  0;
    keepalive_timeout  65;

	#開啟gzip壓縮
    gzip  on;
	gzip_disable "MSIE [1-6].";

	#設定請求緩衝
	client_header_buffer_size    128k;
    large_client_header_buffers  4 128k;
	
	#若需負載均衡請在此處配置叢集
	#upstream  domainName.com {  #伺服器叢集名字  
    #    server    127.0.0.1:8090  weight=1;#伺服器配置   #weight是權重的意思,權重越大,分配的概率越大。  
    #    server    127.0.0.1:8100  weight=2;  
    #}
	
    #server {
    #    listen       80;
    #    server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

    #    location / {
    #        root   html;
    #       index  index.jsp 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;
    #    }
    #}
	
	
	# 888 new add aurorascm.de(shop) https/http2  
	server {
		listen 80;
		server_name  aurorascm.de;
		if ($scheme ~ http) {
			return https://www.aurorascm.de:443$request_uri;
		}
	}
    server {
        listen       443 ssl http2 default_server;
        server_name  aurorascm.de;

        ssl_certificate      /usr/java/nginx/cert/de.pem;
        ssl_certificate_key  /usr/java/nginx/cert/de.key;
		ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_session_cache    shared:SSL:20m;
        ssl_session_timeout  60m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
		
        location / {
			proxy_pass  http://0.0.0.0:8080;
			proxy_set_header Host      $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			expires  3d;
        }
		
		#靜態檔案,nginx處理  
        location ~ ^/(js|css|flash|media|jpg|png|gif|mp4)/ {
            expires 1d;
        }					
    }
	
	# 888 new add aurorascm.eu(admin) http
	server {
	
        listen       80;
        server_name  aurorascm.eu;
		
        location / {
			proxy_pass  http://0.0.0.0:8081;
			proxy_set_header Host      $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			expires  3d;
        }
		
		#靜態檔案,nginx處理  
        location ~ ^/(js|css|flash|media|jpg|png|gif)/ {
            expires 1d;
        }					
    }

}

3、驗證HTTP2是否已啟用

使用高版本的Chrome,按照如下步驟操作

第一步:使用Chrome訪問啟用http2的站點,比如環境為https://192.168.0.107:8443。

第二步:新開TAB頁,在位址列中輸入chrome://net-internals/#http2,檢查HTTP/2 sessions下的表格。

第三步:確認表格裡是否出現了上一步訪問的主機地址,比如192.168.0.107:8443

三、後續補充

1、nginx解除安裝

想解除安裝直接把編譯產生的檔案刪除就可以。

2、nginx新增其它元件

第一步:若是第三方元件,先下載,後 ./configure中新增此項就可以,其它已有的也不能少哦,--add-module=/usr/local/nginx-module/cache-purge;若是nginx自身的./configure中--with-http_stub_status_module就可以。

第二步:make下就可以了。不用make install ,這會覆蓋原先的。

3、要想更改安裝目錄。

./configure 後加入此項就可以 --prefix=/path

4、要優化重點在快取上,但還沒搞好,再續。