欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

windows下nginx如何操作命令

 更新時間:2023年06月01日 08:52:22   作者:與荒野°  
這篇文章主要介紹了windows下nginx如何操作命令,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

windows下nginx操作命令

1:啟動nginx

進入nginx 文件夾

此處使用管理員CMD 鍵入start nginx 看到cmd窗口一閃而過即為啟動成功

2:停止 nginx

stop nginx

3:殺死全部nginx 進程

taskkill /f /t /im nginx.exe(重要)

4:重新加載nginx.conf文件

nginx -s reload

nginx的啟動安裝和常用配置例子

由于自己的之前學習 nginx 只會簡單使用,然后每次配置 nginx 都要找文檔去了解怎么配置,有點麻煩,所以這里記錄下一些常用的nginx 配置和配置的例子,到時候直接 copy 修改即可

nginx 的主要功能為 靜態(tài)文件的服務器、負載均衡、重寫或重定向url、正向代理、反向代理 等。

這里使用的 nginx 版本為 1.16.0

配置文件的主要結(jié)構(gòu)為:

nginx.conf

http{
  # 這個用于負載均衡的配置
  upstream backend {
    server backend1.example.com       weight=5;
    server backend2.example.com:8080;
    server backup2.example.com:8080   backup;
  }
  # 服務配置1,我們一般只要修改這里的信息即可
  server{
    listen 8080;
    location / {
      proxy_pass http://localhost:8080;
    }
  }
  # 這個 server 是服務配置2
  server{
    listen 80;
    location ~ \.(gif|jpg|png)$ {
      proxy_pass http://backend;
    }
  }
  # includ 用來引入 nginx 在其他目錄的配置文件,一般正式的公司項目會這樣使用,并以每個域名或 server 塊分成每個配置文件。
  include cust_conf/mainconf; # 加多一個確定的文件,這樣找不到文件的時候會報錯,以免配置錯目錄 
  includ /data/host/config/*.conf;
}

啟動相關(guān)

window 啟動

cmd命令進入安裝文件;

1、啟動:C:\server\nginx-1.0.2>start nginx 或C:\server\nginx-1.0.2>nginx.exe

注:建議使用第一種,第二種會使你的cmd窗口一直處于執(zhí)行中,不能進行其他命令操作。

nginx 命令行參數(shù)

不像許多其他軟件系統(tǒng),Nginx 僅有幾個命令行參數(shù),完全通過配置文件來配置

  • -c </path/to/config> 為 Nginx 指定一個配置文件,來代替缺省的。
  • -t 不運行,而僅僅測試配置文件。nginx 將檢查配置文件的語法的正確性,并嘗試打開配置文件中所引用到的文件。用法:nginx -t, 這個命令也可以查看nginx文件的所在位置。
  • -v 顯示 nginx 的版本。
  • -V 顯示 nginx 的版本,編譯器版本和配置參數(shù)。

啟動、停止 和重新加載配置

通過執(zhí)行 nginx 的可執(zhí)行文件可以直接啟動 nginx。只要 nginx 啟動了,則可以通過執(zhí)行 nginx 加 -s 參數(shù)來控制 nginx 的一些行為。用法如下:

nginx -s ${signal}

${signal} 可以為以下四個值:

  • stop — fast shutdown 快速停止
  • quit — graceful shutdown 優(yōu)雅停止
  • reload — reloading the configuration file 重新加載配置文件
  • reopen — reopening the log files 重新打開日志文件

例如:可以通過執(zhí)行 nginx -s reload 來重新加載配置文件,使更改過的配置文件生效。

靜態(tài)文件的服務器

前端文件或者圖片服務的部署,一般會使用到這個功能,通過 nginx 服務器來分發(fā)(sering out)文件,然后用戶從網(wǎng)絡(luò)上能夠通過 ip 或 域名直接訪問到

1. 實現(xiàn)訪問 stats-server.kanlon.com 實現(xiàn)將 /data/fr 下的目錄作為靜態(tài)文件訪問

server {
    # 訪問端口81,并且訪問的域名為 stats-server.kanlon.com 使用該配置
    listen 81;
    server_name stats-server.kanlon.com;
    charset utf8;
    # 設(shè)置自動查找index文件為false,避免安全性問題
    autoindex off;
    # 設(shè)置展示首頁的文件
    index  index.html index.htm index.php;
    # 如果是/img/ 路徑,則轉(zhuǎn)發(fā)到專門存放圖片的路徑/data/img/中 
    location /img/ {
       alias /data/img/;
    }
    location / {
        root /data/fr;
    }
}

如果訪問出現(xiàn)錯誤,可以看一下 logs/error.log 的錯誤日志,這里會打印出實際訪問的文件路徑。還有測試 nginx 的時候一定要注意是否啟動了多個 nginx 否則可能更改了配置會以為自己設(shè)置錯誤不生效的(沉重教訓)

負載均衡

通過 nginx 可以實現(xiàn)將請求自動轉(zhuǎn)發(fā)都自己的指定的服務域名上,減少單臺服務請求量和實現(xiàn)服務的高可用。

nginx 上默認是通過 ngx_http_upstream_module 模塊實現(xiàn)

1. 配置訪問 load-balancing-test.kanlon.com:83 則負載均衡到指定三個域名上

負載的三個域名地址:load-balancing-test-1.kanlon.com:84,load-balancing-test-2.kanlon.com:85,load-balancing-test-3.kanlon.com:86

其中 要求 load-balancing-test-3.kanlon.com:86 作為備份服務,load-balancing-test.kanlon.com-1:84 和load-balancing-test-2.kanlon.com:85 的請求數(shù)分布比例為 1:2

# 配置健康檢查,當為502,503,504,404的時候表示服務不可用,60秒內(nèi)有兩個這樣的失敗請求則負載到另一個服務上
proxy_next_upstream http_502 http_503 http_504 http_404 error timeout invalid_header;
upstream balancing {
    server load-balancing-test-1.kanlon.com:84 max_fails=1 fail_timeout=60s  weight=1;
    server load-balancing-test-2.kanlon.com:85 max_fails=2 fail_timeout=60s  weight=2;
    server load-balancing-test-3.kanlon.com:86 backup;
}
server {
    # 訪問端口83,并且訪問的域名為 load-balancing-test.kanlon.com 使用該配置
    listen 83;
    server_name load-balancing-test.kanlon.com;
    # 設(shè)置自動查找index文件為false,避免安全性問題
    autoindex off;
    # 設(shè)置展示首頁的文件
    index  index.html index.htm index.php;
    location / {
        proxy_pass http://balancing;
    }
}
server {
    # 訪問端口84,并且訪問的域名為 load-balancing-test-1.kanlon.com 使用該配置
    listen 84;
    server_name load-balancing-test-1.kanlon.com;
    charset utf8;
    # 設(shè)置自動查找index文件為false,避免安全性問題
    autoindex off;
    # 設(shè)置展示首頁的文件
    index  index.html index.htm index.php;
    location / {
        # 通過在目錄后面加上沒有文件的目錄可以模擬服務不可用
        root /data/nginx/load-balancing/load-balancing-test-1;
    }
}
server {
    # 訪問端口85,并且訪問的域名為 load-balancing-test-2.kanlon.com 使用該配置
    listen 85;
    server_name load-balancing-test-2.kanlon.com;
    charset utf8;
    # 設(shè)置自動查找index文件為false,避免安全性問題
    autoindex off;
    # 設(shè)置展示首頁的文件
    index  index.html index.htm index.php;
    location / {
        # 通過在目錄后面加上沒有文件的目錄可以模擬服務不可用
        root /data/nginx/load-balancing/load-balancing-test-2;
    }
}
server {
    # 訪問端口86,并且訪問的域名為 load-balancing-test-3.kanlon.com 使用該配置
    listen 86;
    server_name load-balancing-test-3.kanlon.com;
    charset utf8;
    # 設(shè)置自動查找index文件為false,避免安全性問題
    autoindex off;
    # 設(shè)置展示首頁的文件
    index  index.html index.htm index.php;
    location / {
        root /data/nginx/load-balancing/load-balancing-test-3;
    }
}

其中的包含了一些 nginx 的健康檢查的指令

max_fails=number 設(shè)定Nginx與服務器通信的嘗試失敗的次數(shù)。在fail_timeout參數(shù)定義的時間段內(nèi),如果失敗的次數(shù)達到此值,Nginx就認為服務器不可用。在下一個fail_timeout時間段,服務器不會再被嘗試。 失敗的嘗試次數(shù)默認是1。設(shè)為0就會停止統(tǒng)計嘗試次數(shù),認為服務器是一直可用的。 你可以通過指令proxy_next_upstream、fastcgi_next_upstream和 memcached_next_upstream來配置什么是失敗的嘗試。 默認配置時,http_404狀態(tài)不被認為是失敗的嘗試。

fail_timeout=time 設(shè)定服務器被認為不可用的時間段以及統(tǒng)計失敗嘗試次數(shù)的時間段。在這段時間中,服務器失敗次數(shù)達到指定的嘗試次數(shù),服務器就被認為不可用。

重寫或重定向 url

nginx 可以將匹配的 url 重定向到另外的 url 去,包含改變地址

指定地址重定向

將 rewrite-local.kanlon.com/rewrite/same/site/** 重定向到 rewrite-local.kanlon.com/rewrite2/same/site/** 和 將 rewrite-local.kanlon.com/rewrite/other/site 重定向到 rewrite-other.kanlon.com/rewrite2/same/site

server {
    # 訪問端口88,并且訪問的域名為 rewrite-local.kanlon.com 使用該配置
    listen 88;
    server_name rewrite-local.kanlon.com;
    charset utf8;
    # 設(shè)置自動查找index文件為false,避免安全性問題
    autoindex off;
    # 設(shè)置展示首頁的文件
    index  index.html index.htm index.php;
    # 轉(zhuǎn)發(fā)到本域名的其它路徑
    location /rewrite/same/site {
        rewrite ^/rewrite/same/site(.*)$ /rewrite2/same/site$1 last;
    }
    # 轉(zhuǎn)發(fā)到另外的域名,如果要 重定向,; 前面加上 permanent (永久重定向 301 即可),默認為 302 臨時重定向
    location /rewrite/other/site {
        rewrite ^/rewrite/other/site(.*)$ http://rewrite-other.kanlon.com:89/rewrite2/same/site$1 permanent;
    }
    location /rewrite2/same/site {
       default_type text/html;
       return 200 "$request_uri";
    }
    location / {
        root /data/nginx/rewrite-test/rewriter-local;
    } 
}
server {
    # 訪問端口89,并且訪問的域名為 rewrite-other.kanlon.com 使用該配置
    listen 89;
    server_name rewrite-other.kanlon.com;
    charset utf8;
    # 設(shè)置自動查找index文件為false,避免安全性問題
    autoindex off;
    # 設(shè)置展示首頁的文件
    index  index.html index.htm index.php;
    location /rewrite2/same/site {
       default_type text/html;
       return 200 "$request_uri";
    }
    location / {
        root /data/nginx/rewrite-test/rewriter-other;
    } 
}

正向代理

正向代理,就是好像我們平常使用的 vpn 那樣,我們的訪問的域名不會變,只不過通過 nginx 來幫我們發(fā)送請求和接收請求,并返回自己客戶端。nginx 默認支持 http 協(xié)議的正向代理,如果要支持 https 需要安裝組件。

nginx 配置如下:

server {
? ? listen ? ? ? 90;
? ? server_name ?forward-agent.kanlon.com;
? ? # 這里配置 DNS 服務器的IP地址
? ? resolver 8.8.8.8;
? ? location / {
? ? ? ? proxy_pass http://$http_host$request_uri;
? ? }
}

然后,再在自己電腦上配置代理服務器和端口,即可完成正向代理。

反向代理

反向代理,則直接訪問 nginx 的域名來替代訪問自己原來的要訪問資源的域名,然后得到原資源的域名返回的結(jié)果

這樣則訪問 reverse-proxy.kanlon.com/abc -> http://127.0.0.1:8080/abc

server {
    listen       91;
    server_name  reverse-proxy.kanlon.com;
    #設(shè)置代理
    location / {
        proxy_pass http://127.0.0.1:8080/;
    }
}

下面的配置則為 訪問 reverse-proxy.kanlon.com/abc -> http://127.0.0.1:8080/test/abc

server {
? ? listen ? ? ? 91;
? ? server_name ?reverse-proxy.kanlon.com;
? ? #設(shè)置代理
? ? location / {
? ? ? ? proxy_pass http://127.0.0.1:8080/test/;
? ? }
}

注意:最好保持 locate 后面有/ 與 proxy_pass 后面有 / ,這樣保持轉(zhuǎn)發(fā)的路徑一樣,不然如果proxy_pass 后面沒 / 的話,例如:

server {
? ? listen ? ? ? 91;
? ? server_name ?reverse-proxy.kanlon.com;
? ? #設(shè)置代理
? ? location / {
? ? ? ? proxy_pass http://127.0.0.1:8080/test;
? ? }
}

則訪問時候會變成 reverse-proxy.kanlon.com/abc -> http://127.0.0.1:8080/testabc

配置支持websocket(添加后不會影響普通請求的)

首先需要在nginx 主文件中,http 塊增加以下配置。表示如果帶有upgrade 頭,則升級為請求,否則不;因為websocket請求是基于http請求來實現(xiàn),會通過普通http請求來升級

? ? ? ? ##
? ? ? ? # websocket Settings
? ? ? ? ##
? ? ? ? # 如果沒有Upgrade頭,則$connection_upgrade為close,否則為upgrade
? ? ? ? map $http_upgrade $connection_upgrade {
? ? ? ? ? default upgrade;
? ? ? ? ? '' close;
? ? ? ? }

另外需要再server -> location 模塊中添加上下面兩個設(shè)置請求頭的配置

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;

具體可以一個例子可以參考下面的配置

server {
? ? listen 80;
? ? server_name abc.kanlon.ink ?superset.kanlon.top;
? ? charset utf8;
? ? index ?index.html index.htm index.php;
? ? location / {
? ? ? ? ?proxy_pass http://localhost:8088;
? ? ? ? ?proxy_http_version 1.1;
? ? ? ? ?proxy_redirect off;
? ? ? ? ?proxy_set_header Host $host;
? ? ? ? ?proxy_set_header X-Real-IP $remote_addr;
? ? ? ? ?proxy_read_timeout 3600s;
? ? ? ? ?proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
? ? ? ? ?# 主要是下面兩個的配置
? ? ? ? ?proxy_set_header Upgrade $http_upgrade;
? ? ? ? ?proxy_set_header Connection $connection_upgrade;
? ? }
}

隱藏文件的實際后綴

有時候我們想讓訪問靜態(tài)html文件的時候,需要隱藏掉后綴.html,可以像下面這樣配置

server {
? ? listen 81;
? ? charset utf8;
? ? autoindex off;
? ? index ?index.html index.htm index.php;
? ? try_files $uri $uri/ /index.html?$query_string;
? ? # 如果是以html或者 htm 結(jié)尾,則直接返回對應文件,以免引起nginx循環(huán)查找
? ? location ~ \.(htm|html)$ {
? ? ? root ?/data/nginx_static;
? ? }
? ? location / {
? ? ? # 如果訪問的文件不存在,則通過訪問時添加后綴來隱藏URL中的后綴
? ? ? if (!-e $request_filename){
? ? ? ? ?rewrite ^(.*)$ /$1.html last;?
? ? ? ? ?break;
? ? ? }?
? ? ? root ?/data/nginx_static;
? ? }
}

這樣的話,假設(shè)項目根目錄下有l(wèi)ogin.html 文件,原只能通過訪問 127.0.0.1/login.html 訪問到,設(shè)置之后,通過 127.0.0.1/login 或者 127.0.0.1/login.html 都能訪問到

使用nginx的一些注意事項

nginx 配置的if函數(shù)不支持嵌套(if(1=1){if(a=b){})和多條件and 、or 等條件拼接(if(1=1 and 2=2))。

如果要實現(xiàn)多條件判斷,一般使用一個中間變量來實現(xiàn),如下示例,就是實現(xiàn)了當訪問域名為根目錄的時候,如果域名地址不是以home開頭的并且?guī)в胁樵儏?shù)時,則強制跳轉(zhuǎn)到對應的域名;

?? ?location = / {
?? ??? ?set $need_redirect 0;
?? ??? ?if ($query_string) {
?? ??? ??? ? ?set $need_redirect 1;
?? ??? ?}
?? ??? ?# 如果是home開頭的域名,則不用跳轉(zhuǎn);否則為其他開頭的域名,都需要跳轉(zhuǎn)到home(并且在查詢參數(shù)存在的時候)
?? ??? ?if ($host ~ "^home(.*)") {?
?? ??? ??? ? ?set $need_redirect 0;
?? ??? ?}
?? ??? ?if ($need_redirect) {
?? ??? ??? ? ?rewrite ^/(.*)$ http://home-test.kanlon.ink/$1 last;
?? ??? ?}
?? ?}

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 通過Nginx代理轉(zhuǎn)發(fā)配置實現(xiàn)跨域的方法(API代理轉(zhuǎn)發(fā))

    通過Nginx代理轉(zhuǎn)發(fā)配置實現(xiàn)跨域的方法(API代理轉(zhuǎn)發(fā))

    這篇文章主要給大家介紹了關(guān)于如何通過Nginx代理轉(zhuǎn)發(fā)配置實現(xiàn)跨域(API代理轉(zhuǎn)發(fā))的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Nginx具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-11-11
  • 前端加載訪問速度優(yōu)化詳細指南(Nginx)

    前端加載訪問速度優(yōu)化詳細指南(Nginx)

    在前端開發(fā)中,優(yōu)化頁面加載速度成為了開發(fā)者的一項重要任務,下面這篇文章主要給大家介紹了關(guān)于前端加載訪問速度優(yōu)化(Nginx)的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-06-06
  • Nginx如何實現(xiàn)對城市以及指定IP的訪問限制

    Nginx如何實現(xiàn)對城市以及指定IP的訪問限制

    本文介紹了如何使用Nginx代理MySQL連接并限制可訪問IP,以及如何通過第三方模塊ngx_http_geoip2_module實現(xiàn)基于國家/城市訪問限制
    2025-03-03
  • linux下 nginx監(jiān)控問題

    linux下 nginx監(jiān)控問題

    這篇文章主要介紹了linux 下nginx監(jiān)控問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • Nginx配置圖片服務器(極簡配置)

    Nginx配置圖片服務器(極簡配置)

    本文主要介紹了主要是Nginx做靜態(tài)圖片服務器的詳情配置說明,還包括做反向代理、動靜分離、負載均衡的極簡配置,具有一定的參考價值,感興趣的可以了解一下
    2023-08-08
  • nginx配置域名轉(zhuǎn)發(fā)到其他域名的幾種方法小結(jié)

    nginx配置域名轉(zhuǎn)發(fā)到其他域名的幾種方法小結(jié)

    本文主要介紹了nginx配置域名轉(zhuǎn)發(fā)到其他域名的幾種方法小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • Nginx 502 Bad Gateway錯誤常見的4種原因和解決方法

    Nginx 502 Bad Gateway錯誤常見的4種原因和解決方法

    這篇文章主要介紹了Nginx 502 Bad Gateway錯誤常見的4種原因和解決方法,本文適用FastCGI環(huán)境,其中多數(shù)原因通過配置相關(guān)參數(shù)即可解決,需要的朋友可以參考下
    2015-05-05
  • 在ubuntu下為nginx配置支持cgi腳本的方案

    在ubuntu下為nginx配置支持cgi腳本的方案

    本文的需求:讓nginx能夠解析.cgi后綴的文件,相信會特意看這篇文章的人對CGI是什么及其作用已經(jīng)有了足夠的了解,所以在這里不再贅述,直接開始配置。
    2017-02-02
  • 詳解Nginx中HTTP的keepalive相關(guān)配置

    詳解Nginx中HTTP的keepalive相關(guān)配置

    這篇文章主要介紹了Nginx中HTTP的keepalive相關(guān)配置,以及Nginx的Httpd守護進程相關(guān)的keepalive timeout配置,需要的朋友可以參考下
    2016-01-01
  • Nginx+Lua腳本+Redis 實現(xiàn)自動封禁訪問頻率過高IP

    Nginx+Lua腳本+Redis 實現(xiàn)自動封禁訪問頻率過高IP

    本文主要介紹了如何使用OpenResty+Lua進行動態(tài)封禁IP的解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-10-10

最新評論