nginx一台服务器布置多个网站

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默认不同的,否则可能会报错。

  1.  
    upstream haha{
  2.  
    server 192.168.1.129:8081;
  3.  
    }
  4.  
    server {
  5.  
    listen 80;
  6.  
    server_name www.haha.com;
  7.  
     
  8.  
    #charset koi8-r;
  9.  
     
  10.  
    #access_log logs/host.access.log main;
  11.  
     
  12.  
    location / {
  13.  
    proxy_pass http://haha;
  14.  
    index index.html index.htm;
  15.  
    }
  16.  
     
  17.  
    }
  18.  
     
  19.  
    upstream e3-mall {
  20.  
    server 192.168.1.129:8080;
  21.  
    #负载均衡 有多少个服务器就加
  22.  
    server 192.168.1.129:8082 weight=2;
  23.  
    }
  24.  
    server {
  25.  
    listen 80;
  26.  
    server_name www.e3-mall.com;
  27.  
     
  28.  
    #charset koi8-r;
  29.  
     
  30.  
    #access_log logs/host.access.log main;
  31.  
     
  32.  
    location / {
  33.  
    proxy_pass http://e3-mall;
  34.  
    index index.html index.htm;
  35.  
    }
  36.  
     
  37.  
    }

  

 

#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即可

转载于:https://www.cnblogs.com/zengjiao/p/10263585.html

 

 

WindowsServer2008配置Nginx1.14.1版本,创建Windwos服务

wanhongbo029 2018-12-24 11:46:05 1367 收藏 1
分类专栏: nginx windows
版权
操作系统版本:Windows Server 2008 R2

Nginx版本:1.14.1

直接解压到D:\server\nginx-1.14.1文件夹下,查看一下nginx.exe的帮助信息。

D:\server\nginx-1.14.1>nginx.exe -h
nginx version: nginx/1.14.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: NONE)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
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;
upstream server{
#ip_hash;
server 127.0.0.1:8080;
#server 127.0.0.1:8012;
}
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
#代理配置参数
proxy_connect_timeout 180;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_set_header Host $host;
proxy_set_header X-Forwarder-For $remote_addr;

#反向代理的路径(和upstream绑定),location 后面设置映射的路径
location / {
proxy_pass http://server;

}
}
直接使用命令行启动,停止nginx

D:\server\nginx-1.14.1>start nginx

D:\server\nginx-1.14.1>nginx.exe -s stop

在Windows任务管理器中可以看到nginx进程的创建和结束,没有问题,但是无法跟随操作系统重启自动启动,尤其是windows有时候自己做完更新就重启了,还要在手动启动一下nginx,似乎很不合理。所以考虑创建windows服务。

使用Windows service wrapper创建服务

https://github.com/kohsuke/winsw/releases,自行下载

将WinSW.NET2.exe重新命名为NginxService.exe,创建一个配置文件NginxServce.xml,

 

<configuration>
<id>Nginx</id>
<name>Nginx Service</name>
<description>Nginx Service (powered by WinSW)</description>
<!-- Path to the executable, which should be started -->
<executable>%BASE%\nginx.exe</executable>
<stoparguments>-s stop</stoparguments>

<logpath>%BASE%\logs</logpath>
<logmode>roll</logmode>
</configuration>
 %BASE%就是指NginxServie.exe所在的目录,跟nginx.exe在同一个目录

注册服务

 

 目前存在的问题:

nginx.conf配置文件中,worker_processes一般设置为跟CPU数量相等,设置为1,通过NginxService可以正常的启动和停止,如果设置为2,通过NginxService就不能正常Kill掉进程,不知道为什么,没有深究,都是开源的程序,可以深究一下源代码。

如果worker_processes设置为2及以上的情况,关闭服务,需要手动Kill ngnix进程,否则无法杀死进程,nginx用作反向http代理,一般也不需要频繁重启,重新启动应用服务器就可以了。

worker_processes  1;
————————————————
版权声明:本文为CSDN博主「wanhongbo029」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wanhongbo029/java/article/details/85229072

posted on 2020-06-28 13:13  曹明  阅读(395)  评论(0)    收藏  举报