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

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_4492.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2018/12/20 10:01:22

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博客
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

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