首頁 > 運維 > Nginx > 主體

nginx HTTP伺服器怎麼安裝與設定

王林
發布: 2023-05-19 20:58:04
轉載
1539 人瀏覽過

http伺服器
因為tomcat處理靜態資源的速度比較慢,所以首先想到的就是把所有靜態資源(js,css,image,swf)
提到單獨的伺服器,用更快速的http伺服器,這裡選擇了nginx了,nginx相比apache,更加輕量級,
配置更加簡單,而且nginx不僅僅是高性能的http伺服器,還是高性能的反向代理伺服器。
目前許多大型網站都使用了nginx,新浪、網易、qq等都使用了nginx,說明nginx的穩定性和性能還是非常不錯的。
1. nginx 安裝(linux)
下載最新穩定版本
根據自己需要的功能先下載對應模板,這裡下載了下面幾個模組:
openssl-0.9 .8l,zlib-1.2.3,pcre-8.00
編譯安裝nginx:
./configure
--without-http_rewrite_module
--with-http_ssl_module
#--with-openssl= ../../lib/openssl-0.9.8l
--with-zlib=../../lib/zlib-1.2.3
--with-pcre=../../ lib/pcre-8.00
--prefix=/usr/local/nginx
make
make install
2、nginx處理靜態資源的設定
#啟動gzip壓縮css和js
gzip on;
# 壓縮等級1-9,預設是1,等級越高壓縮率越大,當然壓縮時間也越長
gzip_comp_level 4;
# 壓縮類型
gzip_types text/css application/x-javascript;
# 定義靜態資源存取的服務,對應的網域名稱:res.abc.com
server {
listen 80;
server_name res .abc.com;
# 開啟伺服器讀取檔案的緩存,
open_file_cache max=200 inactive=2h;
open_file_cache_valid 3h;
open_cache_errors off;
charset utf-8;
open_cache_errors off;
charset utf-8;

判斷如果是圖片或swf,客戶端快取5天
location ~* ^. .(ico|gif|bmp|jpg|jpeg|png|swf)$ {
root /usr/local/resource /;
access_log off;
index index.html index.htm;
expires 5d;
}
# 因js,css改動比較頻繁,客戶端快取8小時
location ~* ^. .(js|css)$ {
root /usr/local/resource/;
access_log off;
index index.html index.htm;
expires 8h;
}
# 其他靜態資源
location / {
root /usr/local/resource;
access_log off;
expires 8h;}}

3、nginx 反向代理設定

# 反向代理服務,綁定網域名稱www.abc.com
server {
listen 80;
server_name www.abc.com;
charset utf-8;
# bbs使用discuz!
# 因反向代理為了提高效能,一部分http頭部資訊不會轉發給後台的伺服器,
# 使用proxy_pass_header 和proxy_set_header 把有所需的http頭部資訊轉寄給後台伺服器
location ^~ /bbs/ {
root html;
access_log off;
index index.php;
# 轉送host的訊息,如果不設定host,在背景使用request.getservername()取到的網域不是www.abc.com,而是127.0.0.1
proxy_set_header host $host;
# 因discuz! 為了安全,需要取得客戶端user-agent來判斷每次post資料是否跟第一次請求來自同1個瀏覽器,
# 如果不轉發user-agent,discuz! 提交資料就會報"您的請求來路不正確,無法提交"的錯誤
proxy_pass_header user-agent;
proxy_pass http://127.0.0.1:8081;
}
# 其他請求轉送至tomcat
location / {
root html ;
access_log off;
index index.jsp;
proxy_pass http://127.0.0.1:8080;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;

}###}#######

以上是nginx HTTP伺服器怎麼安裝與設定的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!