同一伺服器下配置多域名,去掉Tomcat埠號,Nginx反向代理做轉發
1. 開啟nginx,資源管理器檢視開啟是否成功,會有兩個nginx.exe程序,瀏覽器輸入127.0.0.1可以訪問到nginx歡迎介面。
2. 在nginx配置檔案nginx.conf中配置多個虛擬主機,即server,用於存放不同的域名(server_name),虛擬主機分別監聽80埠(預設),轉發到不同的tomcat埠。
3. 將專案分別放在不同的tomcat 下,webapps下刪除所有資料夾,新建資料夾並重命名為ROOT,專案war包解壓(tomcat啟動自動解壓)後的檔案全部拷貝到該ROOT目錄下。
Nginx配置檔案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;
#cgzip on;
upstream website {
#ip_hash;
server xxxx.xxxx.xxxx.xxxx:8080; #監聽伺服器上的tomcat 1埠
}
upstream websiteView {
#ip_hash;
server xxxx.xxxx.xxxx.xxxx:8081; #監聽同一伺服器上的不同tomcat 2埠
}
server {
listen 80;
server_name www.xxxxxxxx.com; #虛擬主機1
#
error_page 500 502 503 504 /50x.html;
#
location = /50x.html {
root html;
}
#
location ^~ /resourceServer/ {
root html;
}
location / {
proxy_pass http://website; #所有請求均攔截
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 128k;
proxy_buffers 2 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
}
location /專案名稱 {
proxy_pass http://website; #轉發到tomcat 2
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 128k;
proxy_buffers 2 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
}
}
server {
listen 80;
server_name www.xxxxx.com; #虛擬主機2
#
error_page 500 502 503 504 /50x.html;
#
location = /50x.html {
root html;
}
#
location ^~ /resourceServer/ {
root html;
}
#
location / {
proxy_pass http://websiteView;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 128k;
proxy_buffers 2 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
}
location /另一個專案名稱 {
proxy_pass http://websiteView; #轉發到tomcat 2
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
proxy_buffer_size 128k;
proxy_buffers 2 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
}
}
這樣,輸入兩個域名便可訪問到不同的專案