1. 程式人生 > >windows下配置nginx反向代理tomcat

windows下配置nginx反向代理tomcat

Nginx下載官方地址:http://nginx.org/en/download.html
下載之後解壓後的目錄結構是這樣的
這裡寫圖片描述
常用的命令:
nginx -v 檢視nginx版本
start nginx啟動nginx命令
nginx -s reload 修改了配置檔案後重新reload
nginx -s stop 立刻停止
nginx -s quit 優雅地停止
啟動成功後,開啟瀏覽器輸入localhost:80埠,出現下面的歡迎頁面說明配置沒問題啟動成功了。
這裡寫圖片描述
例項:
Nginx預設埠是80,假定現在要通過nginx來反向代理後端的埠為8080的tomcat,配置如下

server {
        listen       80
; //監聽的埠號 server_name 192.168.0.238; //server名稱 #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_redirect off; 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_set_header X-Forwarded-Proto $scheme; proxy_pass http://127.0.0.1:8080; } #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; #} }

配置說明:
Host包含客戶端真實的域名和埠號
X-Real-IP 客戶端真實的ip地址
X-Forwarded-For 記錄客戶端真實ip和中間經歷的多層代理的ip集
X-Forwarded-Proto表示客戶端真實的協議(http還是https)
proxy_pass 代理的tomcat的地址(ip+埠或者域名)