配置nginx並創建爲windows服務&&配置

第一步:安裝nginx

               下載windows版nginx (http://nginx.org/en/download.html) ,安裝在E:\software\nginx-1.15.9

 

將nginx配置成爲windows服務

需要藉助"Windows Service Wrapper"小工具,項目地址: https://github.com/kohsuke/winsw

下載地址: http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/1.18/winsw-1.18-bin.exe

下載該工具後,將其放在 Nginx安裝目錄下,並重命名爲nginx-service.exe,創建配置文件nginx-service.xml(名字要和工具名一樣),

創建nginx-service.exe.config(爲支持NET 4.0 runtime,默認只支持NET 2.0 runtime)

文件結構:

nginx-service.xml 配置:

<service>
  <id>nginx</id>
  <name>Nginx Service</name>
  <description>High Performance Nginx Service</description>
  <logpath>E:\software\nginx-1.15.9\logs</logpath>
  <log mode="roll-by-size">
    <sizeThreshold>10240</sizeThreshold>
    <keepFiles>8</keepFiles>
  </log>
  <executable>E:\software\nginx-1.15.9\nginx.exe</executable>
  <startarguments>-p E:\software\nginx-1.15.9</startarguments>
  <stopexecutable>E:\software\nginx-1.15.9\nginx.exe</stopexecutable>
  <stoparguments>-p E:\software\nginx-1.15.9 -s stop</stoparguments>
</service>

nginx-service.exe.config 配置:

<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727" />
    <supportedRuntime version="v4.0" />
  </startup>
  <runtime>
    <generatePublisherEvidence enabled="false"/> 
  </runtime>
</configuration>

在cmd中運行如下命令安裝windows服務(E:\software\nginx-1.15.9\nginx-service.exe  install)

之後就可以在Windows服務面板中啓動服務了

瀏覽器訪問看看

當使用nginx在同一個域名下配置多個項目:這裏只展示1

  1. nginx按不同的目錄分發給不同的項目
  2. 啓用二級域名,不同的項目分配不同的二級域名

注意:這三個項目屬於不同的域名,項目之間通過http訪問會存在跨域問題。

server {
    listen    80;
    server_name example.com;
 
    location ^~ /admin {
        proxy_pass     http://localhost:9092;
        proxy_set_header  Host       $host;
        proxy_set_header  X-Real-IP    $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }
 
    location ^~ /customer {
        proxy_pass     http://localhost:9091;
        proxy_set_header  Host       $host;
        proxy_set_header  X-Real-IP    $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }
 
    location / {
       proxy_pass     http://localhost:9090;
       proxy_set_header  Host       $host;
       proxy_set_header  X-Real-IP    $remote_addr;
       proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    }

 

 

nginx的狀態監控

        通過查看nginx的併發連接,我們可以更清楚的知道網站的負載情況。在Web界面比較精確的查看如下:

在nginx.conf中的server裏面加入

location /nginx_status { stub_status on; access_log off; allow 127.0.0.1;  訪問IP deny all; }

配置完後重新啓動Nginx後我們可以通過瀏覽器訪問 http://loclahost/nginx_status查看,如下圖:

解析:Active connconnections     //當前Nginx正處理的活動連接數。

          server accepts handledrequests   //總共處理了42個連接,成功創建42次握手,總共處理了146個請求。

          Reading     //nginx讀取到客戶端的Header信息數。

          Writing     //nginx返回給客戶端的Header信息數。

         Waiting     //開啓keep-alive的情況下,這個值等於  active-(reading +writing) ,意思就是Nginx已經處理完正在等候下一次請求指令的駐留連接No2

 

root和alias都可以定義在location模塊中,都是用來指定請求資源的真實路徑。。。

       root  :真實的路徑是root指定的值加上location指定的值 。
       alias  :指定的路徑是location的別名,不管location的值怎麼寫,資源的 真實路徑都是 alias 指定的路徑 

其他區別:

    1、 alias 只能作用在location中,而root可以存在server、http和location中。

     2、alias 後面必須要用 “/” 結束,否則會找不到文件,而 root 則對 ”/” 可有可無

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章