nginx一台服務器布置多個網站


通過nginx反向代理可以把ip用類似www.baidu.com顯示出來

1、先在C:\Windows\System32\drivers\etc\HOSTS進行修改,可以用SwitchHosts修改

# leyou
192.168.32.130 www.leyou.com                這些ip是虛擬機的ip
192.168.32.130 manage.leyou.com
192.168.32.130 api.leyou.com

2、修改nginx.conf

顯示nginx

#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;
   #這步的話輸入IP即可直接訪問nginx 比如192.168.1.129
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
    }
#這步的話輸入ip+端口 比如192.168.1.129:81即可訪問到nginx server { listen
81; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html81; //這個html81是自己創建的,意思就是訪問比如192.168.32.130:81的話,會訪問html81下的index.jsp文件,只是為了區分 index index.html index.htm; } }
#這個的話輸入網址即可直接訪問 比如www.e3-mall.com
#前提是SwitchHost里面已經配置好了ip和域名的映射關系 server { listen
80; server_name www.e3-mall.com; //這個因為在SwitchHosts修改了配置文件,所以這里需要修改下 #charset koi8-r; #access_log logs/host.access.log main; location / { root html-e3; //這個html-e3是自己創建的,意思就是訪問比如www.e3-mall.com的話,會訪問html-e3下的index.jsp文件,只是為了區分
index index.html index.htm; } } }

****如果修改了nginx文件,需要在nginx的啟動目錄重新刷新一遍:nginx -s reload 

反向代理:

顯示tomcat

安裝完tomcat后,輸入ip+端口號即可顯示tomcat,如192.168.1.129:8080   如果直接輸入192.168.1.129,那么會訪問到nginx,因為我們在上面已經配置了

下面就是反向代理,加在nginx.conf就可以了

比如我們訪問www.haha.com,因為已經在switchhost配置了域名了,所以訪問www.haha.com的時候,nginx會反向代理到192.168.1.129:8081上,注意8081是我們在tomcat修改了對應的端口號,

<Server port="8005">   <Connector port="8080"> <Connector port="8009"> 這些都需要修改,即如果修改成81端口,8005和8009也得換成和tomcat默認不同的,否則可能會報錯。

upstream haha{
       server 192.168.1.129:8081;
      }
    server {
        listen       80;
        server_name  www.haha.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass   http://haha;
            index  index.html index.htm;
        }
        
    }

    upstream e3-mall {
    server 192.168.1.129:8080;
    #負載均衡 有多少個服務器就加
    server 192.168.1.129:8082 weight=2;
    }
    server {
        listen       80;
        server_name  www.e3-mall.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://e3-mall;
            index  index.html index.htm;
        }
        
    }

  

 

#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;
    sendfile        on;
    keepalive_timeout  65;

    gzip  on;

    server {
        listen       80;
        server_name  manage.leyou.com;

        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
            proxy_pass http://192.168.32.1:9001;  #主機地址下的9001端口,就是自己電腦的ip
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        }
   }
        server {
        listen       80;
        server_name  api.leyou.com;

        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
            proxy_pass http://192.168.32.1:10010;
            proxy_connect_timeout 600;
            proxy_read_timeout 600;
        }
    }
}

3、重啟配置文件,在nginx目錄輸入 nginx -s reload即可


免責聲明!

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



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