茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1670947 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_0098.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

軟體應用 : [轉貼]Nginx 在 windows 環境下的安裝與簡單配置

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Nginx 在 windows 環境下的安裝與簡單配置

Nginx在windows環境下的安裝與簡單配置

一. 下載並安裝Nginx

Nginx官網下載

我這裏選取 nginx/Windows-1.10.3版本,下載後解壓出來即可,解壓出來的路徑不能含有中文

我解壓後將其放置的路徑如下

 

 二、開始運行

在當前目錄下按住shift+鼠標右鍵,選擇“在此處打開命令窗口”,然後輸入start nginx

此時,就可以進入瀏覽器輸入訪問地址,http://127.0.0.1/或者http://localhost/即可訪問


三、配置文檔講解

核心配置文檔就是nginx.conf,該文檔位於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;
#}
}
# 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;
# }
#}
}

  

 其中#代表註釋

nginx我們最主要的作用是拿來做反向代理和負載均衡,這個我後面會着重講解。同時它還是一個web服務器,與我們常用的Apache、tomcat、IIS一樣,也可以用來託管web服務。

本章先暫時介紹下該配置文檔中的幾個重要參數,後面會對nginx部署php和Python項目再進行着重講解,至於java的項目通常是tomcat+nginx同時進行配置,nginx用來做負載均衡和處理靜態頁。

1、定義Nginx運行的用户和用户組

#user  nobody;

2、nginx進程數,建議設置為等於CPU總核心數

worker_processes 1;

3、全局錯誤日誌定義類型,[ debug | info | notice | warn | error | crit ]


#error_log logs/error.log notice;
#error_log logs/error.log info;

4、進程文檔

#pid        logs/nginx.pid;

5、工作模式與連接數上限:worker_connections是單個後台worker process進程的最大併發鏈接數,併發總數是 worker_processes 和 worker_connections 的乘積, 即 max_clients = worker_processes * worker_connections


events {
worker_connections 1024;
}

6、http下的一些配置及其意義


include mime.types; #文檔擴展名與文檔類型映射表
default_type application/octet-stream; #默認文檔類型
sendfile on; #開啟高效文檔傳輸模式,sendfile指令指定nginx是否調用sendfile函數來 輸出文檔,對於普通應用設為 on,如果用來進行下載等應用磁盤IO重負載應用,可設置 為off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常 把這個改成off。
autoindex on; #開啟目錄列表訪問,合適下載服務器,默認關閉。
tcp_nopush on; #防止網絡阻塞
tcp_nodelay on; #防止網絡阻塞
keepalive_timeout 120; #長連接超時時間,單位是秒
gzip on; #開啟gzip壓縮輸出

7、server虛擬主機的相關配置

我們平時配置各類服務器,配置最多的就是這些地方了

比如:


http{
#虛擬主機1
server{
listen 80; #監聽端口,基於IP配置的時候變更此處,比如192.168.1.100:8080;
server_name www.xdw.com; #主機域名,實際項目發佈的話,填公網上的域名,本地部署的話,可以在C:\Windows\System32\drivers\etc\hosts文檔中添加IP和域名的映射
location / { #映射解析,/代表根路徑,此處解析還有正則表達式的解析方式,具體請參考http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_core_module.html#location
root E:/xdw/0221; #工程所在路徑
index index.html index.htm; #首頁(默認頁)
}
}
#虛擬主機2,可以同時配置多個虛擬主機
server{
listen 8080;
server_name localhost;
location / {
root D:/xiangmu/txym_web;
index index.html index.htm;
}
}
}

  看到這個虛擬主機的配置,相信配置過tomcat或者Apache的人都很熟悉的感覺。此篇就到此結束,下面還會更新linux下的配置,php和python項目的部署,反向代理和負載均衡,配合tomcat部署java項目。


原文出處:Nginx在windows環境下的安裝與簡單配置 - 掃文資訊
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Windows 下安裝配置 Nginx 詳解

windows 下安裝配置 Nginx 詳解

nginx 功能之一可以啟動一個本地服務器,通過配置 server_name root 目錄等來訪問目標文件

儘管通過這種方式能實現分佈式文件存儲,但也存在弊端,就是 FTP 很容易被入侵,而且小型的網站使用 FTP 作為文件服務器是沒問題的,但是項目訪問量持續增加的話,必要考慮文件服務器的擴展性與高可用,目前成熟的文件服務器也有很多,例如 FastDFS, 可以快速的進行線性擴容。

1.下載Nginx

http://nginx.org/

下載後解壓,如下圖:

2.Nginx配置

找到 conf 目錄裡的 nginx.conf 文件,配置 Nginx

2.1.基本配置如下:


#user  nobody;
#指定nginx進程數
worker_processes 1;
#全局錯誤日誌及PID文件
#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服務器,利用它的反向代理功能提供負載均衡支持
http {
#設定mime類型,類型由mime.type文件定義
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 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,
sendfile on;
#tcp_nopush on;
#連接超時時間
#keepalive_timeout 0;
keepalive_timeout 65;
#開啟gzip壓縮 ,壓縮html
#gzip on;
#設定負載均衡的服務器列表 支持多組的負載均衡,可以配置多個upstream 來服務於不同的Server.
#nginx 的 upstream 支持 幾 種方式的分配
#1)、輪詢(默認) 每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器down掉,能自動剔除。
#2)、weight 指定輪詢幾率,weight和訪問比率成正比,用於後端服務器性能不均的情況。 跟上面樣,指定了權重。
#3)、ip_hash 每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。
#4)、fair
#5)、url_hash #Urlhash
upstream mysvr {
#weigth參數表示權值,權值越高被分配到的幾率越大
#1.down 表示單前的server暫時不參與負載
#2.weight 默認為1.weight越大,負載的權重就越大。
#3.backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這台機器壓力會最輕。
#server 192.168.1.116 down;
#server 192.168.1.116 backup;
server 192.168.1.121 weight=1;
server 192.168.1.122 weight=2;
}
#配置代理服務器的地址,即Nginx安裝的服務器地址、監聽端口、默認地址
server {
#1.偵聽80端口
listen 80;
#對於server_name,如果需要將多個域名的請求進行反向代理,可以配置多個server_name來滿足要
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# 默認主頁目錄在nginx安裝目錄的html子目錄。
root html;
index index.html index.htm;
proxy_pass http://mysvr; #跟載均衡服務器的upstream對應
}
#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;
# }
#}
}


2.2.server配置


2.3.可以配置多個server

如下,這樣訪問 localhost 就能訪問到了 D:/source 目錄


http {
#靜態文件
server {
listen 80;
server_name static.com;
location / {
root D:/source ;
}
}
#html文件
server {
listen 80;
server_name 127.0.0.1 localhost;
location / {
root D:/source ;
index index.html index.htm;
}
}
}


3.啟動Nginx

注意不要直接雙擊nginx.exe,這樣會導致修改配置後重啟、停止nginx無效,需要手動關閉任務管理器內的所有nginx進程

cmd 進入 Nginx 解壓目錄 執行以下命令

start nginx : 啟動nginx服務

nginx -s reload :修改配置後重新加載生效

nginx -s reopen :重新打開日誌文件
nginx -t -c /path/to/nginx.conf 測試nginx配置文件是否正確

啟動後如何檢查是否啟動成功呢? 輸入命令 看到以下信息說明啟動成功了

一切就緒,訪問一下 server 裡配置的 server_name 是不是被重定向到 upstream 配置的服務器上了,是不是很簡單!

4.nginx常用命令:

驗證配置是否正確 : nginx -t

查看 Nginx 的版本號: nginx -V

啟動 Nginx start nginx

快速停止或關閉 Nginx
nginx -s stop

正常停止或關閉 Nginx nginx -s quit

配置文件修改重裝載命令: nginx -s reload

5.常見錯誤

如果啟動失敗 可以看下 logs 目錄下 error.log 文件裡的錯誤信息。

我在第一次安裝的時遇到兩個錯誤,也是最容易碰到的問題,在這裡列出來方便大家碰到相同的問題時快速解決。

1.端口佔用問題

我的配置文件裡服務偵聽的是 80 端口,由於機器上部署了 IIS 80 端口被默認站點佔用,把站點關閉就可以了,這個問題在錯誤日誌裡記錄是這樣的。

2015/01/15 10:44:12 [emerg] 8800#5988: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

碰到類似的錯誤,請確認端口是否被佔用或被防火牆屏蔽

2.Nginx所在目錄有中文

錯誤日誌大致輸出一下內容

2015/01/15 11:55:55 [emerg] 5664#8528: CreateFile() "E:\
軟件

3. 啟用緩存時報錯

2015/01/15 17:26:50 [emerg] 17068#20356: shared zone "cache_one" has no equal addresses: 02CF0000 vs 02A20000

2015/01/15 17:26:50 [alert] 11536#11228: worker process 17068 exited with code 1

我一直沒有找到解決的方法,有人說重啟服務,或者緩存設置大一點就可以了,我試了一下沒有用的, 官網 原文是這樣講的,只能認為 windwos 下無解了。

: The cache and other modules which require shared memory support do

: not work in Windows Vista and later due to address space layout

: randomization being enabled in these Windows versions.

4.缺少nginx.pid文件

nginx: [error] CreateFile() "E:\nginx\nginx-1.9.3/logs/nginx.pid" failed


nginx: [error] CreateFile() "E:\nginx\nginx-1.9.3/logs/nginx.pid" failed

nginx: [error] Open() "E:\nginx\nginx-1.9.3/logs/nginx.pid" failed

解決方法 :

使用命令創建 /logs/nginx.pid 文件 :

nginx -c conf/nginx.conf

5.bash: nginx: command not found

有可能是你再 linux 命令行環境下運行了 windows 命令,

如果你之前是允許 nginx -s reload 報錯, 試下 ./nginx -s reload

或者 用 windows 系統自帶命令行工具運行

6.windows下nginx訪問web目錄提示403 Forbidden

  在 windows http 服務器 nginx 時,訪問 web 目錄提示 403 Forbidden ,首先需要瞭解 nginx 出現 403 錯誤是什麼意思:

   403 Forbidden 表示你在請求一個資源文件但是
nginx 不允許你查看, 403 Forbidden 只是一個 HTTP 狀態碼,像 404,200 一樣不是技術上的錯誤。

找到 nginx.conf

  將 user nobody 改為 user root; 重啟 ng ,仍無效果。

後發現 nginx 默認是不支持瀏覽目錄的。

找到 autoindex off 更改為 on 。 重啟 ng, 正常訪問。

另外 Linux 下,nginx.conf 配置文件最上面的那個 「# user nobody」 要改成 「user ftpuser」 ,不然訪問可能會出現 403 錯誤。


原文出處: windows 下安装配置 Nginx 详解 - kingscoming的博客 - CSDN博客
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]在windows 環境下的安裝、配置、使用以及設置為 windows 服務自啟動

Nginx 教程一:在windows環境下的安裝、配置、使用以及設置為windows服務自啟動

目前國內各大門戶網站已經部署了Nginx,如新浪、網易、騰訊等;國內幾個重要的視頻分享網站也部署了Nginx,如六房間、酷6等。新近發現Nginx 技術在國內日趨火熱,越來越多的網站開始部署Nginx。

相比apeach、iis,nginx以輕量級、高性能、穩定、配置簡單、資源佔用少等優勢廣受歡迎。

下載地址:

   http://nginx.org

啟動

  解壓至c:\nginx,運行nginx.exe(即nginx -c conf\nginx.conf),默認使用80端口,日誌見文件夾C:\nginx\logs; 若nginx.exe啟動一閃而過,則需要修改nginx.conf中的默認端口

使用

   http://localhost:端口

關閉

  nginx -s stop 或taskkill /F /IM nginx.exe > nul

常用配置

  C:\nginx\conf\nginx.conf,使用自己定義的conf文件如my.conf,命令為nginx -c conf\my.conf


  常用配置如下:
  Nginx.conf代碼
  http {
   server {
   #1.偵聽80端口
   listen 80;
   location / {
   # 2. 默認主頁目錄在nginx安裝目錄的html子目錄。
   root html;
   index index.html index.htm;
   # 3. 沒有索引頁時,羅列文件和子目錄
   autoindex on;
   autoindex_exact_size on;
   autoindex_localtime on;
   }
   # 4.指定虛擬目錄
   location /tshirt {
   alias D:\programs\Apache2\htdocs\tshirt;
   index index.html index.htm;
   }
   }
   # 5.虛擬主機www.emb.info配置
   server {
   listen 80;
   server_name www.emb.info;
   access_log emb.info/logs/access.log;
   location / {
   index index.html;
   root emb.info/htdocs;
   }
   }
  }
  
  http {
   server {
   #1.偵聽80端口
   listen 80;
   location / {
   # 2. 默認主頁目錄在nginx安裝目錄的html子目錄。
   root html;
   index index.html index.htm;
   # 3. 沒有索引頁時,羅列文件和子目錄
   autoindex on;
   autoindex_exact_size on;
   autoindex_localtime on;
   }
   # 4.指定虛擬目錄
   location /tshirt {
   alias D:\programs\Apache2\htdocs\tshirt;
   index index.html index.htm;
   }
   }
   # 5.虛擬主機www.emb.info配置
   server {
   listen 80;
   server_name www.emb.info;
   access_log emb.info/logs/access.log;
   location / {
   index index.html;
   root emb.info/htdocs;
   }
   }
  }
  
  小提示:
  運行nginx -V可以查看該Win32平台編譯版支持哪些模塊。我這裡的結果為:
  Log代碼
  nginx version: nginx/0.7.65
  TLS SNI support enabled
  configure arguments:
  --builddir=objs.msvc8
  --crossbuild=win32
  --with-debug --prefix=
  --conf-path=conf/nginx.conf
  --pid-path=logs/nginx.pid
  --http-log-path=logs/access.log
  --error-log-path=logs/error.log
  --sbin-path=nginx.exe
  --http-client-body-temp-path=temp/client_body_temp
  --http-proxy-temp-path=temp/proxy_temp
  --http-fastcgi-temp-path=temp/fastcgi_temp
  --with-cc-opt=-DFD_SETSIZE=1024
  --with-pcre=objs.msvc8/lib/pcre-7.9
  --with-openssl=objs.msvc8/lib/openssl-0.9.8k
  --with-openssl-opt=enable-tlsext
  --with-zlib=objs.msvc8/lib/zlib-1.2.3
  --with-select_module
  --with-http_ssl_module
  --with-http_realip_module
  --with-http_addition_module
  --with-http_sub_module
  --with-http_dav_module
  --with-http_stub_status_module
  --with-http_flv_module
  --with-http_gzip_static_module
  --with-http_random_index_module
  --with-http_secure_link_module
  --with-mail
  --with-mail_ssl_module
  --with-ipv6
  
  nginx version: nginx/0.7.65
  TLS SNI support enabled
  configure arguments:
  --builddir=objs.msvc8
  --crossbuild=win32
  --with-debug --prefix=
  --conf-path=conf/nginx.conf
  --pid-path=logs/nginx.pid
  --http-log-path=logs/access.log
  --error-log-path=logs/error.log
  --sbin-path=nginx.exe
  --http-client-body-temp-path=temp/client_body_temp
  --http-proxy-temp-path=temp/proxy_temp
  --http-fastcgi-temp-path=temp/fastcgi_temp
  --with-cc-opt=-DFD_SETSIZE=1024
  --with-pcre=objs.msvc8/lib/pcre-7.9
  --with-openssl=objs.msvc8/lib/openssl-0.9.8k
  --with-openssl-opt=enable-tlsext
  --with-zlib=objs.msvc8/lib/zlib-1.2.3
  --with-select_module
  --with-http_ssl_module
  --with-http_realip_module
  --with-http_addition_module
  --with-http_sub_module
  --with-http_dav_module
  --with-http_stub_status_module
  --with-http_flv_module
  --with-http_gzip_static_module
  --with-http_random_index_module
  --with-http_secure_link_module
  --with-mail
  --with-mail_ssl_module
  --with-ipv6
  
  顯然,最經常用的memcache, rewrite模塊都沒在其中,因此該win32編譯版本僅能供基本開發測試使用,對於產品平台,應該重新編譯自己想要的win32版本,或者在linux下使用更方便。


簡單原理

配置文件基本結構就是這樣子,由若干指令(directives)構成。指令分為簡單指令(siple directives)和塊指令(block directives)。


簡單指令由指令名和參數構成,指令名和參數以空格分隔,每條指令以分號結尾。例如

user nginx;

這就是一條簡單指令,表示以 nginx 這個用戶身份運行 nginx 工作進程。指令名為 user ,參數為 nginx,最後分號結束。


塊指令由指令名和若干由花括號{}包圍起來的一組指令組成。例如

events {

worker_connections 1024;

}

就是一個塊指令,指令名為 events,後面緊跟 {} 包圍起來的一組指令。

如果一個塊指令內有其他指令,那麼這個塊指令也成為上下文(context),不在任何上下文中的指令被認為是在主上下文中(main context)。例如 events 和 http 位於主上下文中,server 位於 http 上下文中,location 則位於 server 上下文中。

以井號#開頭的行是註釋行,不起作用。

查看nginx進程

映像名稱 PID 會話名 會話# 內存使用
========================= ======== ================ =========== ============
nginx.exe 8944 Console 1 5,128 K
nginx.exe 6712 Console 1 5,556 K

nginx常用命令


nginx -s stop 強制關閉
nginx -s quit 安全關閉
nginx -s reload 改變配置文件的時候,重啟nginx工作進程,來時配置文件生效
nginx -s reopen 打開日誌文件

nginx設置為windows自啟動

每次手動啟動和關閉nginx.exe比較麻煩,因此可以在 windows系統下將nginx作為系統服務啟動。

a . 準備工作


下載安裝nginx,並記住安裝目錄 官網下載

下載winsw, 下載地址 (http://www.cr173.com/soft/101797.html)

b. winsw設置

將winsw可執行程序複製到nginx安裝目錄下,並重命名為nginx-service

新建名為nginx-service.xml的文件(註:文件名必須與可執行文件名相同)

並編輯如下,其中name為 服務名,executable為可執行程序路徑,logpath為程序運行日誌路徑

[html] view plain copy
  1. < service >
  2. < id > nginx </ id >
  3. < name > nginx </ name >
  4. < description > nginx </ description >
  5. < executable > E:\phpStudy\nginx\nginx.exe </ executable >
  6. < logpath > E:\phpStudy\nginx\ </ logpath >
  7. < logmode >
    roll </ logmode >
  8. < depend > </ depend >
  9. < startargument > -p E:\phpStudy\nginx </ startargument >
  10. < stopargument > -p E:\phpStudy\nginx -s stop </ stopargument >
  11. </ service >

如下:

c. 安裝服務

在nginx安裝目錄下運行cmd(快捷方式:shift + 鼠標右鍵),運行命令:nginx-service.exe install


註:nginx-service.exe uninstall命令可刪除對應的系統服務

nginx-service.exestop 命令可停止 對應的系統服務

nginx-service.exe start命令可啟動對應的系統服務

d. 查看服務是否安裝成功

計算機管理 -> 服務


如服務為未運行狀態,可在此啟動服務,或設置為自動啟動

註:若服務安裝成功,可在cmd(管理員身份)中對服務進行如下操作

啟動nginx :net start nginx

停止nginx:net stop nginx

e. 驗證nginx是否正常運行

在瀏覽器中打開網址http://localhost

原文地址:http://www.cnblogs.com/chuncn/archive/2011/10/14/2212291.html



原文出處:Nginx 教程一:在windows环境下的安装、配置、使用以及设置为windows服务自启动 - Aeroleo的博客 - CSDN博客
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]在 windows 環境下的安裝、配置、使用以及設置為 windows 服務自啟動

在 windows 環境下的安裝、配置、使用以及設置為 windows 服務自啟動

目前國內各大門戶網站已經部署了Nginx,如新浪、網易、騰訊等;國內幾個重要的視頻分享網站也部署了Nginx,如六房間、酷6等。新近發現Nginx 技術在國內日趨火熱,越來越多的網站開始部署Nginx。

相比apeach、iis,nginx以輕量級、高性能、穩定、配置簡單、資源佔用少等優勢廣受歡迎。

下載地址:

   http://nginx.org

啟動

  解壓至c:\nginx,運行nginx.exe(即nginx -c conf\nginx.conf),默認使用80端口,日誌見文件夾C:\nginx\logs; 若nginx.exe啟動一閃而過,則需要修改nginx.conf中的默認端口

使用

   http://localhost:端口

關閉

  nginx -s stop 或taskkill /F /IM nginx.exe > nul

常用配置

  C:\nginx\conf\nginx.conf,使用自己定義的conf文件如my.conf,命令為nginx -c conf\my.conf


  常用配置如下:
  Nginx.conf代碼
  http {
   server {
   #1.偵聽80端口
   listen 80;
   location / {
   # 2. 默認主頁目錄在nginx安裝目錄的html子目錄。
   root html;
   index index.html index.htm;
   # 3. 沒有索引頁時,羅列文件和子目錄
   autoindex on;
   autoindex_exact_size on;
   autoindex_localtime on;
   }
   # 4.指定虛擬目錄
   location /tshirt {
   alias D:\programs\Apache2\htdocs\tshirt;
   index index.html index.htm;
   }
   }
   # 5.虛擬主機www.emb.info配置
   server {
   listen 80;
   server_name www.emb.info;
   access_log emb.info/logs/access.log;
   location / {
   index index.html;
   root emb.info/htdocs;
   }
   }
  }
  
  http {
   server {
   #1.偵聽80端口
   listen 80;
   location / {
   # 2. 默認主頁目錄在nginx安裝目錄的html子目錄。
   root html;
   index index.html index.htm;
   # 3. 沒有索引頁時,羅列文件和子目錄
   autoindex on;
   autoindex_exact_size on;
   autoindex_localtime on;
   }
   # 4.指定虛擬目錄
   location /tshirt {
   alias D:\programs\Apache2\htdocs\tshirt;
   index index.html index.htm;
   }
   }
   # 5.虛擬主機www.emb.info配置
   server {
   listen 80;
   server_name www.emb.info;
   access_log emb.info/logs/access.log;
   location / {
   index index.html;
   root emb.info/htdocs;
   }
   }
  }
  
  小提示:
  運行nginx -V可以查看該Win32平台編譯版支持哪些模塊。我這裡的結果為:
  Log代碼
  nginx version: nginx/0.7.65
  TLS SNI support enabled
  configure arguments:
  --builddir=objs.msvc8
  --crossbuild=win32
  --with-debug --prefix=
  --conf-path=conf/nginx.conf
  --pid-path=logs/nginx.pid
  --http-log-path=logs/access.log
  --error-log-path=logs/error.log
  --sbin-path=nginx.exe
  --http-client-body-temp-path=temp/client_body_temp
  --http-proxy-temp-path=temp/proxy_temp
  --http-fastcgi-temp-path=temp/fastcgi_temp
  --with-cc-opt=-DFD_SETSIZE=1024
  --with-pcre=objs.msvc8/lib/pcre-7.9
  --with-openssl=objs.msvc8/lib/openssl-0.9.8k
  --with-openssl-opt=enable-tlsext
  --with-zlib=objs.msvc8/lib/zlib-1.2.3
  --with-select_module
  --with-http_ssl_module
  --with-http_realip_module
  --with-http_addition_module
  --with-http_sub_module
  --with-http_dav_module
  --with-http_stub_status_module
  --with-http_flv_module
  --with-http_gzip_static_module
  --with-http_random_index_module
  --with-http_secure_link_module
  --with-mail
  --with-mail_ssl_module
  --with-ipv6
  
  nginx version: nginx/0.7.65
  TLS SNI support enabled
  configure arguments:
  --builddir=objs.msvc8
  --crossbuild=win32
  --with-debug --prefix=
  --conf-path=conf/nginx.conf
  --pid-path=logs/nginx.pid
  --http-log-path=logs/access.log
  --error-log-path=logs/error.log
  --sbin-path=nginx.exe
  --http-client-body-temp-path=temp/client_body_temp
  --http-proxy-temp-path=temp/proxy_temp
  --http-fastcgi-temp-path=temp/fastcgi_temp
  --with-cc-opt=-DFD_SETSIZE=1024
  --with-pcre=objs.msvc8/lib/pcre-7.9
  --with-openssl=objs.msvc8/lib/openssl-0.9.8k
  --with-openssl-opt=enable-tlsext
  --with-zlib=objs.msvc8/lib/zlib-1.2.3
  --with-select_module
  --with-http_ssl_module
  --with-http_realip_module
  --with-http_addition_module
  --with-http_sub_module
  --with-http_dav_module
  --with-http_stub_status_module
  --with-http_flv_module
  --with-http_gzip_static_module
  --with-http_random_index_module
  --with-http_secure_link_module
  --with-mail
  --with-mail_ssl_module
  --with-ipv6
  
  顯然,最經常用的memcache, rewrite模塊都沒在其中,因此該win32編譯版本僅能供基本開發測試使用,對於產品平台,應該重新編譯自己想要的win32版本,或者在linux下使用更方便。


簡單原理

配置文件基本結構就是這樣子,由若干指令(directives)構成。指令分為簡單指令(siple directives)和塊指令(block directives)。


簡單指令由指令名和參數構成,指令名和參數以空格分隔,每條指令以分號結尾。例如

user nginx;

這就是一條簡單指令,表示以 nginx 這個用戶身份運行 nginx 工作進程。指令名為 user ,參數為 nginx,最後分號結束。


塊指令由指令名和若干由花括號{}包圍起來的一組指令組成。例如

events {

worker_connections 1024;

}

就是一個塊指令,指令名為 events,後面緊跟 {} 包圍起來的一組指令。

如果一個塊指令內有其他指令,那麼這個塊指令也成為上下文(context),不在任何上下文中的指令被認為是在主上下文中(main context)。例如 events 和 http 位於主上下文中,server 位於 http 上下文中,location 則位於 server 上下文中。

以井號#開頭的行是註釋行,不起作用。

查看nginx進程

映像名稱 PID 會話名 會話# 內存使用
========================= ======== ================ =========== ============
nginx.exe 8944 Console 1 5,128 K
nginx.exe 6712 Console 1 5,556 K

nginx常用命令


nginx -s stop 強制關閉
nginx -s quit 安全關閉
nginx -s reload 改變配置文件的時候,重啟nginx工作進程,來時配置文件生效
nginx -s reopen 打開日誌文件

nginx設置為windows自啟動

每次手動啟動和關閉nginx.exe比較麻煩,因此可以在 windows系統下將nginx作為系統服務啟動。

a . 準備工作


下載安裝nginx,並記住安裝目錄 官網下載

下載winsw, 下載地址(http://www.cr173.com/soft/101797.html)

b. winsw設置

將winsw可執行程序複製到nginx安裝目錄下,並重命名為nginx-service

新建名為nginx-service.xml的文件(註:文件名必須與可執行文件名相同)

並編輯如下,其中name為 服務名,executable為可執行程序路徑,logpath為程序運行日誌路徑

[html] view plain copy
  1. < service >
  2. < id > nginx </ id >
  3. < name > nginx </ name >
  4. < description > nginx </ description >
  5. < executable > E:\phpStudy\nginx\nginx.exe </ executable >
  6. < logpath > E:\phpStudy\nginx\ </ logpath >
  7. < logmode >
    roll </ logmode >
  8. < depend > </ depend >
  9. < startargument > -p E:\phpStudy\nginx </ startargument >
  10. < stopargument > -p E:\phpStudy\nginx -s stop </ stopargument >
  11. </ service >

如下:

c. 安裝服務

在nginx安裝目錄下運行cmd(快捷方式:shift + 鼠標右鍵),運行命令:nginx-service.exe install


註:nginx-service.exe uninstall命令可刪除對應的系統服務

nginx-service.exestop 命令可停止 對應的系統服務

nginx-service.exe start命令可啟動對應的系統服務

d. 查看服務是否安裝成功

計算機管理 -> 服務


如服務為未運行狀態,可在此啟動服務,或設置為自動啟動

註:若服務安裝成功,可在cmd(管理員身份)中對服務進行如下操作

啟動nginx :net start nginx

停止nginx:net stop nginx

e. 驗證nginx是否正常運行

在瀏覽器中打開網址http://localhost


windows下nginx安装、配置与使用 - chuncn - 博客园
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Nginx Virtual Host 設定教學

Nginx Virtual Host 設定教學

最近剛好有網友詢問 Nginx Virtual Host 相關設定,那我就直接記錄一下設定方式。Virtual Host 主要的作用在於使用同一台 HTTP Server 架設多個站台,其實就是市面上常見的「虛擬主機」。技術上是透過多重 Domain Name 指向同一個 IP 來達成,然而對於 HTTP Server 則會透過 HTTP Request Header 中的 Host 來識別要派送到那一台機器,封包如下:


Nginx 設定 Virtual Host 的方式蠻簡單的,我們依照 Ubuntu 慣例將設定檔放在 /etc/nginx/sites-available/ 目錄中,再將設定檔案透過 ln 建立 link 到 /etc/nginx/sites-enabled/。如果是 RedHat, CentOS 設定檔案直接在 /etc/nginx/conf.d/ 即可。我們先規劃兩個站台分別為 vh1.toright.com 與 vh2.toright.com。設定檔如下:

/etc/nginx/sites-enabled/vh-1

1
2
3
4
5
6
7
8
9
10
11
12
server {
         listen 80 ;
 
         root / var / www
/ html / vh1 ;
         index index . html index . htm ;
 
         server_name vh1 . toright . com ;
 
         location / {
                 try _files $ uri $ uri / = 404
;
         }
}

/etc/nginx/sites-enabled/vh-2

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
         listen 80 ;
 
         root / var / www / html / vh2 ;
 
         index index . html index . htm ;
 
         server_name vh2 . toright . com ;
 
         location / {
                 try _files $ uri $ uri / = 404 ;

         }
}

建立好設定檔後透過以下命令重新啟動 nginx

sudo service nginx restart

查一下我們要測試的 Domain 是指向同一個 IP Address,如下:

然後開啟瀏覽器分別進入這兩個網域:

成功囉,下次見!


原文出處:Nginx Virtual Host 設定教學 - Soul & Shell Blog
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Nginx – 設定虛擬主機 (Virtual Host) 的方式
Nginx – 設定虛擬主機 (Virtual Host) 的方式

目前的需求狀況是,兩個網域要指向同一個 IP 的設定。
範例
imjsn.com 指向 A主機 127.0.0.1
www.imjsn.com 也要指向 A主機 127.0.0.1。

雲端主機是 CenterOS 6.5。

假設 jsn 是我的帳號名稱。注意,有些主機不叫做 vhost.conf 可能會叫做 virtual.conf 之類。
網頁路徑
/virtualhost/jsn/網頁都放在這底下,如 index.html


每台主機設定都有差異,多數會放置在如
/var/www/html/這裡


就看你怎麼去自行設定。
統一設定檔
/usr/local/nginx/conf/vhost.conf


這裡面可能會出現 include 這樣的關鍵字,用來引用它處的設定檔,這是為了方便分門別類管理。當遇到不同的網域或網址,會採用不同的設定檔。
include /usr/local/nginx/conf/vhost/imjsn/imjsn.c53196; // 可能是預設的。這裡引用沒有 www 的設定檔。後面的 c53196 大概是我這邊系統自己產生添加的亂數編號。
include /usr/local/nginx/conf/vhost/www.imjsn/www.imjsn.c53196; // 這裡引用有 www 的網域設定。因為這是我自己添加的,所以我接著要到 vhost 底下自己建立一個路徑 /www.imjsn/

*這個方法是分開各自的設定檔。有些作法是寫在同一個檔案裡;分開來我覺得是比較好懂比較不凌亂。
各別設定檔 (每個網域都一個)

imjsn.com 設定在此 (我雲端主機設定好一開始就有的):
/usr/local/nginx/conf/vhost/imjsn/imjsn.c53196;


www.imjsn.com 設定在此:
/usr/local/nginx/conf/vhost/www.imjsn/www.imjsn.c53196


如果我打算將 www.mjson.com 導向到 imjsn.com 就會長這樣
server {

    location ~ ^(.+\.php)(.*)$ {
        fastcgi_pass 127.0.0.1:7027 ;
        include fastcgi.conf ;
    }

    root /virtualhost/imjsn;

    access_log /usr/local/nginx/logs/imjsn-imjsn.c53196 yundns_log ;

    index index.php default.php index.htm default.html index.html ;

    server_name imjsn.com ;

    location ~ \.(mdf|sql|bak)$ {
        return 404 ;
    }
}

server {

    server_name www.imyolo.com ;

    rewrite ^/(.*) http://imyolo.com/$1 permanent;

}

如果你想讓 IP (如 21.216.11.111 ) 也能自動導向到 imjsn.com ,那也是使用這樣的設定,只要多增加一個就好了。 這樣統一個對外的網域,在 SEO 與流量上會比較有利喔!
解釋
    server_name  :  輸入設定的網域名稱
    rewrite : 輸入對應的網域名稱
    access_log : 輸入access_log 的存放路徑
    error_log : 輸入error_log 的存放路徑
    root   :  設定此網域的網頁資料所存放的地方
    index  : 設定首頁能讀的項目


Nginx – 設定虛擬主機 (Virtual Host) 的方式 | jsnWork
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]多個 virtual host 共用一個 Nginx Site Config

多個 virtual host 共用一個 Nginx Site Config

目前在網站開發時,已有固定的開發部署流程,我們會依據 development -> staging -> production 的順序部署。在比較小型的專案,有時為了節省資源,會將多個 development 網站部署在同一台 VPS,透過給予不同的專案 domain,再配合 Nginx 做出 virtual host 來運行。

其實 Nginx 在 Config 檔案之中,可以使用一些簡易的判斷式及變數,因此雖然是多個專案使用同一台 VPS,但也只需要設定一次 Nginx Site Config 檔即可。


實作的方式如下:
首先規劃出固定的 domain 及 folder 路徑規則。

domain 規則為 XXXXX.dev.site
舉例:projectA.dev.site、projectB.dev.site

Folder 規則為 /var/www/XXXXX/
舉例:/var/www/projectA/ 、 /var/www/projectB/

接著在 Nginx 中,建立一個 Site Config,並其中有兩個地方要使用變數,分別是 server_name、root,可以參閱下面的範例。
server_name ~^(.*)\.dev\.site$;
set $project $1;
root /var/www/$project/;

如此一來,只要是從任何吻合此正規表達式的 domain 進來,Nginx 會將 domain 的第一個單字丟進變數 $1,接著再新增變數 $project = $1,第三行告知 Nginx 要去何處取得此網站檔案,檔案路徑中則包含了變數 $project。

假設是從 abc.dev.site 進來,則 $project 就會等於 abc。

透過上述的方式,即可達到只用一個 Site Config 就能運行多個不同的網站,也就不用每次新專案啟動或專案結束,又要手動管理一次 development 機的 Nginx Site Config。

其實在 server_name 可以取得不只一個變數,例如改成下面的格式,即可根據 domain 拿到兩個變數。
server_name ~^(.*)\.(.*)\.site$; set $var1 $1; set $var2 $2;
因此同樣假設是從 abc.dev.site 進來,則 $var1 會等於 abc、$var2 則會等於 dev。


2015/4/22 補充說明:
發現有人寫了一篇更好的除了 Nginx 也說明了 Apache 該如何做,補上來當參考資料。

參考資料:


原文出處:艦長,你有事嗎?: 多個 virtual host 共用一個 Nginx Site Config
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]CentOS 6 建立 Nginx Reverse Proxy

CentOS 6 建立 Nginx Reverse Proxy

Nginx 很常拿來做 Proxy / Reverse Proxy,設定方便又簡單

通常 Reverse Proxy 會放在非交戰區(DMZ) 來做為 Public 跟 Private 之間的連線,避免 attack 直接接觸到資料主機,也能降低資料主機的 Loading

 

範例架構

 

試想將 Web Server 放置內網,所有使用者都必須由 Reverse Proxy 代為交涉

 

為了瞭解概念,用 Nginx官網 來當作 Web Server

而我們必須要從 Reverse Proxy 來反向代理到 Nginx官網

 

 

環境

CentOS 6.6

Reverse Proxy: 172.16.10.10

目標 Web : http://nginx.com

 

 

安裝 Nginx Reverse Proxy

step1. 先把 Nginx 安裝起來

要從EPEL才能找到 Nginx




1
$ yum install nginx

 

 

step2. nginx.conf 常態性調整



1
2
3
4
5
6
7
8
$ vim / etc / nginx / nginx . conf
user               nginx ;
worker _processes    2 ;
worker_cpu _affinity 01 10 ;
events {
     use epoll ;
    
worker _connections    1024 ;
}

 

 

 

step3. 設定 Reverse Proxy



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ vim / etc / nginx / conf . d / default . conf
 
server {
     listen        80 ;
     #Proxy的位置,提供給使用者連線
    
server _name 172.16.10.10 ;
     location / {
         root    / usr / share / nginx / html ;
         index   index . html index . htm ;
        
         #後端的Web伺服器,以Nginx官網為例
        
proxy_pass http : //nginx.com;
 
         #定義header記錄使用者IP
         proxy_set _header X - Real - IP $ remote_addr ;
        
         #讓後端的Web伺服器可以取得使用者真實IP
         proxy_set _header X - Forwarded - For $ proxy_add_x_forwarded_for ;
 
         #把 Protocol header 也往後送
         proxy_set _header X - Forwarded - Proto $ http_x_forwarded_proto ;
         proxy_max_temp_file _size 0 ;
     }
}

 

 

其實設定檔很簡單,其常常忘記的就是忘記加上 $proxy_add_x_forwarded_for

若是沒有加上這個,在後端的 Web Server 取得的 access IP 都會是 Proxy Server 的 IP,而不是真實IP。

 

在後端如果也是使用 Nginx 的話,必須在 nginx.conf 加上一些設定

若是前端的 IP header 為 X-Forwarded-For

那就要設定為




1
2
3
4
$ vim / etc / nginx / nginx .conf
 
set_real_ip _from 125.119.141.65 ;
real_ip _header X - Forwarded - For ;

 

 

set_real_ip_from 就是前端的 Proxy IP

read_ip_header 是前端記錄真實 IP 的 header,必須相符合


原文出處:CentOS 6 建立 Nginx Reverse Proxy | Mr. 沙先生
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]設定 nginx 為 reverse cache server
設定 nginx 為 reverse cache server

Nginx(發音同 engine x)是一款由俄羅斯程式設計師 Igor Sysoev 所開發輕量級的網頁伺服器、反向代理伺服器以及電子郵件(IMAP/POP3)代理伺服器。起初是供俄國大型的入口網站及搜尋引擎 Rambler(俄語:Рамблер)使用。此軟體 BSD-like 協定下發行,可以在 UNIX、GNU/Linux、BSD、Mac OS X、Solaris,以及 Microsoft Windows等作業系統中執行。
步驟
1.安裝 CentOS
請參考 http://www.snippetinfo.net/media/235
設定部分: http://www.snippetinfo.net/media/240

2.設定 yum
# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

3.使用 yum 安裝 nginx
# yum install nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror01.idc.hinet.net
 * extras: mirror01.idc.hinet.net
 * updates: mirror01.idc.hinet.net
nginx                                                                                                                                                     | 2.9 kB     00:00
nginx/primary_db                                                                                                                                          |  24 kB     00:00
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:1.4.2-1.el6.ngx will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================================
 Package                                Arch                                    Version                                             Repository                              Size
=================================================================================================================================================================================
Installing:
 nginx                                  x86_64                                  1.4.2-1.el6.ngx                                     nginx                                  311 k

Transaction Summary
=================================================================================================================================================================================
Install       1 Package(s)

Total download size: 311 k
Installed size: 770 k
Is this ok [y/N]: y
Downloading Packages:
nginx-1.4.2-1.el6.ngx.x86_64.rpm                                                                                                                          | 311 kB     00:01
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : nginx-1.4.2-1.el6.ngx.x86_64                                                                                                                                  1/1
----------------------------------------------------------------------

Thanks for using NGINX!

Check out our community web site:
* http://nginx.org/en/support.html

If you have questions about commercial support for NGINX please visit:
* http://www.nginx.com/support.html

----------------------------------------------------------------------
  Verifying  : nginx-1.4.2-1.el6.ngx.x86_64                                                                                                                                  1/1

Installed:
  nginx.x86_64 0:1.4.2-1.el6.ngx

Complete!

4.建立 nginx所使用 的 cache 目錄
mkdir /var/nginx
mkdir /var/nginx/cache

5.設定 nginx.conf
其中 proxy_pass http://xxx.xxx.xxx.xxx; 要設定你要指向的 proxy ip
# vi /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {

    proxy_cache_path  /var/nginx/cache  levels=1:2    keys_zone=STATIC:10m
                                         inactive=24h  max_size=1g;
     client_max_body_size 10G;
     server {
        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass             http://xxx.xxx.xxx.xxx;
            proxy_set_header       Host $host;
            proxy_cache            STATIC;
            proxy_cache_valid      200  1d;
            proxy_cache_use_stale  error timeout invalid_header updating
                                   http_500 http_502 http_503 http_504;
        }
    }

    include /etc/nginx/conf.d/*.conf;
}

6.
如果要對應多站台的話
# vi /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {

    proxy_cache_path  /var/nginx/cache  levels=1:2    keys_zone=STATIC:10m
                                         inactive=24h  max_size=1g;

    client_max_body_size 10G;
    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  600;
    tcp_nodelay        on;


    upstream backend {
        server {your-backend-server1}:80;
        server {your-backend-server2}:80;
    }

    server {
        location / {
            add_header X-Proxy-Cache    $upstream_cache_status;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass                  http://backend;
            proxy_set_header            Host $host;

            proxy_cache_key             "$scheme$host$request_uri";
            proxy_cache                 STATIC;
            proxy_cache_valid           200  1d;
            proxy_cache_use_stale       error timeout invalid_header updating
                                        http_500 http_502 http_503 http_504;

            proxy_connect_timeout       6000;
            proxy_send_timeout          6000;
            proxy_read_timeout          6000;
            proxy_buffer_size           4k;
            proxy_buffers               4 32k;
            proxy_busy_buffers_size     64k;
            proxy_temp_file_write_size  64k;
            send_timeout                6000;
            proxy_buffering             off;
        }


        gzip on;
        gzip_min_length 1k;
        gzip_buffers 4 16k;
        #gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
        gzip_vary off;
        gzip_disable "MSIE [1-6]\.";
    }

    include /etc/nginx/conf.d/*.conf;
}

7.啟動服務並設為自動啟動
# service nginx restart
# chkconfig nginx on


原文出處:設定 nginx 為 reverse cache server | 老洪的 IT 學習系統
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Nginx 加上 SSL 設定 (reverse proxy mode)
Nginx 加上 SSL 設定 (reverse proxy mode)

如果已經用 nginx 架好 reverse proxy (架設方式: http://snippetinfo.net/media/671/)
然後要加上 SSL 的話,比較簡單的方法是設定好之後,放在 /etc/nginx/conf.d 下面
這樣一來,一個站一個 SSL,就可以很清楚不會混淆了!

設定方式如下:
先在 /etc/nginx/conf/nginx.conf 加上
upstream ssl_backend {
        server w.x.y.z:4443;
}

ssl_session_cache   shared:SSL:10m;
ssl_session_timeout 10m;

然後在 /etc/nginx/conf.d/ 下面丟一個你自己網域的 conf 檔,設定好後重啟 nginx 即可! (e.g: snippetinfo.net.conf)
server {
    listen              443 ssl;

    # if you wanna to enable HTTP/2 (need nginx 1.9.5+)
    #listen              443 ssl http2;
    #listen              [::]:443 ssl http2;


    server_name         *.snippetinfo.net;
    ssl_certificate     /etc/httpd/conf/ssl/snippetinfo.net.crt;
    ssl_certificate_key /etc/httpd/conf/ssl/snippetinfo.net.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    location / {
        add_header X-Proxy-Cache    $upstream_cache_status;
        proxy_pass                  https://ssl_backend;
        proxy_set_header            Host $host;
        proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_cache_key             "$scheme$host$request_uri";
        proxy_cache                 STATIC;
        proxy_cache_valid           200  7d;
        proxy_cache_bypass  $http_cache_control;
        proxy_cache_use_stale       error timeout invalid_header updating
                                    http_500 http_502 http_503 http_504;

    }

}


原文出處:Nginx 加上 SSL 設定 (reverse proxy mode) | 老洪的 IT 學習系統
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|