实现Nginx的的简单案例教程
1、首先在虚拟机的linux系统中按照之前教程,安装好Nginx
2、配置同样的Linux三台,一定要安装好tomcat服务器
3、修改tomcat的端口
进入tomcat目录的conf目录下,把server.xml打开,把其中的8080改成8081即可
4、修改好三台之后,打开终端,运行ifconfig 记录好每台机器的IP地址
5、在终端中运行下面的命令 关闭防火墙。
service iptables stop
永久关闭修改配置开机不启动防火墙:
chkconfig iptables off
6、在每一个tomcat -webapp下中放入一个war包可以通过地址访问
子主题 1
子主题 2
子主题 3
进入tomcat目录的conf目录下,把server.xml打开,把其中的8080改成8081即可
7、打开装好的Nginx目录进入,进入conf目录,打开Nginx.conf 修改代理的服务器地址和监听端口
把下面的东西加入http文件中(Nginx002为自己放在tomcat中的web项目名,另外三个为三台Linux的地址和它所对应的端口) upstream Nginx002 {
server 192.168.153.128:8081;
server 192.168.153.129:8082;
server 192.168.153.130:8083;
} 把监视端口改成8080,把它改成这样 server {
listen 8080;
server\_name localhost;
#charset koi8-r;
#access\_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy\_pass http://Nginx002;
}
整体如备注文件
8、完成之后保存,重启Nginx,Tomcat(三台机器要在同一局域网内,并可以ping通)。开始测试在一台机器中 输入 http://localhost:8080/Nginx002/shiyi.html 重复点回车可以看到在一个服务器可以访问的三个页面在表示部署成功
配置好的Nginx.conf文件
1 #user nobody;
2 worker_processes 1;
3
4 #error_log logs/error.log;
5 #error_log logs/error.log notice;
6 #error_log logs/error.log info;
7
8 #pid logs/nginx.pid;
9
10
11 events {
12 worker_connections 1024;
13 }
14
15
16 http {
17
18
19 include mime.types;
20 default_type application/octet-stream;
21
22 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
23 # '$status $body_bytes_sent "$http_referer" '
24 # '"$http_user_agent" "$http_x_forwarded_for"';
25
26 #access_log logs/access.log main;
27
28 sendfile on;
29 #tcp_nopush on;
30
31 #keepalive_timeout 0;
32 keepalive_timeout 65;
33
34 #gzip on;
35 upstream Nginx002 {
36
37
38 server 192.168.153.128:8081;
39 server 192.168.153.129:8082;
40 server 192.168.153.130:8083;
41
42
43
44 }
45 server {
46 listen 8080;
47 server_name localhost;
48
49 #charset koi8-r;
50
51 #access_log logs/host.access.log main;
52
53 location / {
54 root html;
55 index index.html index.htm;
56 proxy_pass http://Nginx002;
57 }
58
59 #error_page 404 /404.html;
60
61 # redirect server error pages to the static page /50x.html
62 #
63 error_page 500 502 503 504 /50x.html;
64 location = /50x.html {
65 root html;
66 }
67
68 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
69 #
70 #location ~ \.php$ {
71 # proxy_pass http://127.0.0.1;
72 #}
73
74 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
75 #
76 #location ~ \.php$ {
77 # root html;
78 # fastcgi_pass 127.0.0.1:9000;
79 # fastcgi_index index.php;
80 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
81 # include fastcgi_params;
82 #}
83
84 # deny access to .htaccess files, if Apache's document root
85 # concurs with nginx's one
86 #
87 #location ~ /\.ht {
88 # deny all;
89 #}
90 }
91
92
93 # another virtual host using mix of IP-, name-, and port-based configuration
94 #
95 #server {
96 # listen 8000;
97 # listen somename:8080;
98 # server_name somename alias another.alias;
99
100 # location / {
101 # root html;
102 # index index.html index.htm;
103
104 # }
105 #}
106
107
108 # HTTPS server
109 #
110 #server {
111 # listen 443 ssl;
112 # server_name localhost;
113
114 # ssl_certificate cert.pem;
115 # ssl_certificate_key cert.key;
116
117 # ssl_session_cache shared:SSL:1m;
118 # ssl_session_timeout 5m;
119
120 # ssl_ciphers HIGH:!aNULL:!MD5;
121 # ssl_prefer_server_ciphers on;
122
123 # location / {
124 # root html;
125 # index index.html index.htm;
126 # }
127 #}
128
129 }