【學習Nginx-03】Nginx實現反向代理

Nginx實現反向代理

編輯配置文件

user  root;
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

	
	# =============BEGIN 反向代理 =============
	server {
		listen 83;
		server_name localhost;
		
		location / {
			proxy_pass http://localhost:8080;
		}

    }
	
	# =============END 反向代理 =============

}

這裏主要配置了2個地方

  • listen 後面是要監聽本機的端口
  • proxy_pass, 後面接的是要代理的網址

說明

這裏使用本機的83端口代理8080端口的Tomcat服務器,即實現在瀏覽器訪問83端口,nginx將請求轉發到8080端口,顯示tomcat的頁面,而83端口是沒有開服務的。

重新加載配置

sbin/nginx -s reload

訪問測試

先訪問8080端口,http://192.168.1.20:8080/

image-20200424211524141

訪問83端口,http://192.168.1.20:83/

image-20200424211728660

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