如何下載 Nginx (windows 版本)並且簡單的使用


官網地址:http://nginx.org/

進到官網

我這里下載的是 穩定版的 windows版本。

開始我們的簡單測試

步驟一:找到nginx的壓縮包,(隨意找個地方)解壓

步驟二:進入conf文件夾

步驟三:在F盤中 新建一個test.html 文件 (這里是方便測試) !!!!! ---->  靜態資源

步驟四:修改nginx.conf文件

nginx.conf 文件 修改如下

步驟五:進行訪問從而找到靜態資源

  1、找到nginx的安裝目錄 然后 cmd 直接進入命令行窗口

  2、start nginx (啟動 Nginx )

  3、進行訪問我們剛剛弄的test.html 靜態資源

  

  4、結果如下:

  瀏覽器輸入 localhost:80/test.html

  可直接獲取訪問到的資源

步驟六:訪問后端API

  1、找到一個前后端分離的項目(比較好演示而已),只需后端項目的Tomcat啟動就好了

  2、比如我的后端項目配置如下

  

   3、然后 啟動 項目 (我這里使用的是 idea)

   4、修改 nginx.conf 文件

  加一段語法

  proxy_pass http://server:port /;  ---> 后端Tomcat訪問的真實地址

  5、修改完成后、保存 ,來到 cmd命令窗口

  輸入 nginx -s reload

  

進行訪問結果如下:

 

那怎么看 我的請求是否被nginx代理了呢?

很簡單的

如圖:

 

本次演示 nginx.conf 文件完整配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#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指令指定nginx是否調用sendfile函數來輸出文件
     #對於普通應用設為 on,如果用來進行下載等應用磁盤IO重負載應用,可設置為off
     #以平衡磁盤與網絡I/O處理速度,降低系統的負載
     #注意:如果圖片顯示不正常把這個改成off
     sendfile        on;
     #tcp_nopush     on;
 
     #keepalive_timeout  0;
     
     #長連接超時時間,單位是秒
     keepalive_timeout  65;
 
     #啟用Gizp壓縮
     #gzip  on;
     
     #當前的Nginx的配置
     server {
         #監聽80端口,可以改成其他端口
         listen       80;
         
         server_name  localhost;#當前服務的域名
 
         #charset koi8-r;
 
         #access_log  logs/host.access.log  main;
 
         #訪問頁面
         location /{
             root F:/;#根目錄
             index test.html;
         }
         
         #訪問Tomcat API
         location /api {
             #proxy_set_header   Host             $host;
             #proxy_set_header   X-Real-IP        $remote_addr;
             #proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
             proxy_pass http://192.168.6.184:8888/;
         }
 
         #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;
     #    }
     #}
 
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM