nginx配置文件+本地測試請求轉發到遠程服務器+集群


1 在本地測試1

  眾所周知,nginx是一個反向代理的服務器,主要功能即為實現負載均衡和動靜分離。在別的我別的文章有詳細的nginx(Windows)相關介紹教程。

由於自己安裝的nginx在本地的計算機上,想實現對nginx.conf 配置文件的修改實現轉發到遠程服務器,見下圖標記處,為我在配置文件添加的內容,

 

  首先在本地啟動nginx【即在nginx根目錄雙擊nginx.exe ,可以在任務管理器中查看是否有nginx.exe進程,如果有說明開啟成功】,然后根據上圖中的location,在瀏覽器訪問http://localhost/ap/ 【因為是本地且server_name =  localhost,如果是在遠程服務器使用nginx,則localhost改為服務器對應的域名或ip地址】,會請求轉發到百度的首頁。如果是想訪問到上圖中http://XXXX.pub:20180/wlsweb, 則需要修改配置location = /ap/wlsweb/ 或location /ap/wlsweb/【經我自行測試 = 可有可不有】。且后面的/不能省去。注意修改完配置文件,關閉nginx的進程,重新啟動nginx,配置愛文件才會重新加載生效。最后說一點,對應nginx的啟動和關閉也有想過命令,有興趣的可以網上查詢相關命令進行操作。

2 本地測試2

    在本地沒有域名的時候,可以修改本地系統文件進行域名和ip地址的配置進行虛擬。

  1)打開電腦磁盤在系統的目錄下找到hosts文件,我的是在C:\Windows\System32\drivers\etc目錄下,你的應該在X:\Windows\System32\drivers\etc下。

  

  打開hosts文件在其下面添加域名和ip地址,由於是虛擬的,域名可以自行擬定,我是寫的xiha.com  ,當然你可以haha.com等,對於ip地址的添加是計算機連接網絡的ip地址,打開win+r組合件運行輸入cmd進入dos命令窗口輸入ipconfig進行查找。具體內容看下圖。

       

我在hosts文件中添加的內容為10.23.57.227  xiha.com.

  2)nginx的相關配置文件。配置文件是在nginx根目錄下的conf文件夾下的nginx.conf文件。內容如下:

 

#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        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;


    
    #gzip  on;
  upstream tserver{       server 10.23.57.227:8080;#localhost也可以  注意10.23.57.227 為電腦連接外網的ip地址。
        #server 10.23.57.227:8080/aaa/index.html/ }
  server { listen 80;
    erver_name  xiha.com;
     location / {
       index  index.html index.htm;
           proxy_pass  http://tserver;#代理,轉發到集群;
 }   }
    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://tserver;#代理,轉發到集群,優先執行proxy_pass;
        }
       location /ap/ { 
             #proxy_pass http://sandbox.criot.zhangda.pub:28180/wlsweb/; 
         proxy_pass https://www.baidu.com/; 
 }
        
        #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;
    #    }
    #}

}  

 上面配置文件中的紅色部分是我在原配置文件上所添加的 內容,對於集群的配置可以配置多,個反向代理服務器也可也配置多個。

對於集群的配置: 

  upstream tserver{ 
        server 10.23.57.227:8080;#localhost也可以  注意10.23.57.227 為電腦連接外網的ip地址。
	    #server 10.23.57.227:8080/aaa/index.html/
   }

其中的tserver名字也可以自行擬定,但是需要與下面紅色標記處的proxy_pass值一樣。經測試,server 10.23.57.227:8080/aaa/index.html/這樣的格式不行,只支持ip地址+端口號。

反向代理的配置 :

server {
    listen       80;
    erver_name  xiha.com;
     location / {
       index  index.html index.htm;
           proxy_pass  http://tserver;#代理,轉發到集群;
        }
}

 

對於nginx.conf可以配置多個server和upstream,我配置了2個server同時監聽80端口,當在nginx啟動的時候,訪問server_name:localhost 和 xiha.com,都成功轉發到

10.23.57.227:8080,成功訪問到tomcat頁面,因為我本地端口8080是對應的Tomcat,因此要確保tomcat服務啟動。見下圖,成功訪問到Tomcat。當你出現不同的錯誤的時候,要去看目錄logs文件夾下error.log文件中的內容。

 

 


免責聲明!

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



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