關(guān)于Nginx的好與壞,我覺得沒有必要去介紹了,在這里主要分享一下我在實(shí)際的項(xiàng)目部署中是如何快速的調(diào)教N(yùn)ginx的。其中分享的源碼大家可以作為模板代碼,根據(jù)自身項(xiàng)目的實(shí)際情況,酌情使用。 這里簡(jiǎn)單的說一說我為什么要寫這篇文章,網(wǎng)上有很多大而全的文章在介紹Nginx是什么,如何入門等等,玩了很多的文字游戲,反正我接觸Nginx的時(shí)候,去查閱文檔給我的是這種感覺,大而全,但是很亂。這里我要講的不是Nginx的理論知識(shí),而是一些能夠快速的應(yīng)用到項(xiàng)目中的實(shí)際技巧。廢話就說這么多,開始本次分享的主體。 調(diào)教一:開啟GZIP,提高頁(yè)面加載速度 http:{ ... gzip on; gzip_min_length 10; gzip_comp_level 4; gzip_disable 'MSIE [1-10] \.'; gzip_types text/plain appliaton/x-javascript text/css application/xml image/jpeg image/gif image/png image/svg+xml; ...} gzip on開啟gzip壓縮功能gzip_min_lenght 10壓縮臨界值,大于10KB的文件才壓縮gzip_com_level 4設(shè)置壓縮級(jí)別[0-10],數(shù)字越大,壓縮比越好,但消耗的時(shí)間越長(zhǎng)gzip_desable 'MSIE [1-10].'對(duì)IE瀏覽器不采用壓縮,[1-10]表示瀏覽器版本范圍gzip_types需要進(jìn)行文件壓縮的類型,根據(jù)自身情況酌情添加
調(diào)教二:無www的域名跳轉(zhuǎn)到帶www的域名 server{ listen 80; server_name http://; return 301 http://www.$request_uri;}
網(wǎng)上有關(guān)這個(gè)問題提供了另外一種解決辦法,代碼如下: server{ listen 80; server_name www.; if ( $host !='www.'){ rewrite ^/(.*)$ http://www./$1 permanent; } rewrite ^/(.*)$ http://$host$1 permanent;}
調(diào)教三:配置https 關(guān)于如何配置server(http)這里不再介紹,網(wǎng)上相關(guān)文檔很多,這里主要分享如何在Nginx中配置HTTPS,配置代碼如下: server{ listen 443 ssl; server_name www.; access_log logs/com_youdomain_logs.log; ssl_certificate c:/sslfile/cert.crt; ssl_certificate_key c:/sslfile/cert.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; location /{ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto https; proxy_set_header X-real-IP $remote_addr; proxy_set_header X-Forwarded-proto $scheme; proxy_connect_timeout 240; proxy_send_timeout 240; proxy_read_timeout 240; proxy_pass http://localhost:8080; proxy_redirect ~^http://([^:]+)(:\d+)?(.*)$ https://$1$3; }} 這里需要注意幾個(gè)地方:
要實(shí)現(xiàn)https加密請(qǐng)求,還需要web容器的配合,在這里以Apache Tomcat配置為例進(jìn)行介紹。 調(diào)教三:開啟tomcat對(duì)https請(qǐng)求的支持 在上一小節(jié)中,我們對(duì)server的代理做了如下的配置: server{ ... location /{ ... proxy_pass http://localhost:8080; ... } ...} 首先,我們需要將tomcat的連接器(Connector)的端口設(shè)置為8080,將轉(zhuǎn)發(fā)重定向的端口(redirectPort)和代理端口(proxyPort)設(shè)置為443。具體的配置代碼如下: ...<Connector prot='8080' protocol='HTTP/1.1' connectionTimeout='20000' redirectPort='443' proxyPort='443'/>... 然后,需要在Host配置中設(shè)置remoteIpHeader、protocolHeader和protocolHeaderHttpsValue這三個(gè)屬性的值,詳細(xì)配置如下: ... <Host name='localhost' appBase='webapps' ....> <Value className='org.apache.catalina.values.RemoteIpValue' remoteIpHeader='X-Forwarded-For' protocolHeader='X-Forwarded-Proto' protocolHeaderHttpsValue='https'/> ... ... <Context docBase='' path='' reloadable='false'></Context> </Host>... 以上就是就是Nginx+tomcat的組合方式開啟https請(qǐng)求的調(diào)教過程。 作者:譚朝紅 原文地址:https://www./articles/nginx_quick_conf.html 結(jié)束語(yǔ) 以上就是我在實(shí)際項(xiàng)目開發(fā)過程中任務(wù)比較常用其重要的幾個(gè)調(diào)教點(diǎn),希望本次分享能夠幫到你。此次文章主要分享關(guān)于Nginx小而精的一些常用配置技巧,更多的配置如分布式下一服多實(shí)例的配置我會(huì)單獨(dú)些一篇文章進(jìn)行分享,今天的內(nèi)容就到這里結(jié)束了;再次感謝你的拜讀,拜拜~~ |
|