apache端口88 tomcat端口8080
多個(gè).conf方法(優(yōu)點(diǎn)是靈活仰美,缺點(diǎn)就是站點(diǎn)比較多配置起來(lái)麻煩)
這里以配置2個(gè)站點(diǎn)(2個(gè)域名)為例雪营,n 個(gè)站點(diǎn)可以相應(yīng)增加調(diào)整鸿染,假設(shè):
IP地址: 10.10.10.1
域名1 example1.com 放在 /webapp/example1
域名2 example2.com 放在 /webapp/example2
配置 nginx virtual hosting 的基本思路和步驟如下:
把2個(gè)站點(diǎn) example1.com, example2.com 放到 nginx 可以訪問的目錄 /webapp/
給每個(gè)站點(diǎn)分別創(chuàng)建一個(gè) nginx 配置文件 example1.com.conf,example2.com.conf, 并把配置文件放到 /usr/local/nginx/vhosts/
然后在 nginx.conf 里面加一句 include 把步驟2創(chuàng)建的配置文件全部包含進(jìn)來(lái)(用 * 號(hào))
重啟 nginx
具體過(guò)程
下面是具體的配置過(guò)程:
1麻裳、在 /usr/local/nginx 下創(chuàng)建 vhosts 目錄 mkdir /usr/nginx/vhosts
2口蝠、在 /usr/local/nginx/vhosts/ 里創(chuàng)建一個(gè)名字為 example1.com.conf 的文件,把以下內(nèi)容拷進(jìn)去
server {
listen 80;
server_name example1.com www.example1.com; access_log /webapp/example1/logs/access_ example1.log;
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
# 動(dòng)態(tài)頁(yè)面津坑,交給tomcat處理
location ~ \.(jsp|jspx|do|action)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://tomcat_proxy;
}
# 動(dòng)態(tài)頁(yè)面妙蔗,交給apache處理
location ~ \.(php)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://apache_proxy;
}
location /training/ {
proxy_pass http://tomcat_proxy;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#sub_filter /training/ /;
}
# 用戶瀏覽器端的緩存設(shè)置
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 7d;
}
location ~ .*\.(js|css)?$ {
expires 24h;
}
location / {
root /webapp/example1/www;
index index.html index.htm index.php index.jsp;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
}
location ~ /.ht {
deny all;
}
}
3、打開 /usr/local/nginx/conf/nginix.conf 文件疆瑰,在相應(yīng)位置加入 include 把以上文件包含進(jìn)來(lái)
# main server config (http part)
http {
include mime.types;
default_type application/octet-stream;
#關(guān)閉http header 中關(guān)于服務(wù)器的版本號(hào)
#server_tokens off;
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;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 3m;
client_body_buffer_size 512k;
# 代理的相關(guān)參數(shù)設(shè)置
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
# 啟用gzip壓縮眉反,提高用戶訪問速度
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
# 配置需要代理的tomcat
upstream tomcat_proxy{
ip_hash;
session_sticky;
server localhost:8080 max_fails=3 weight=1 fail_timeout=60s;
}
# 配置需要代理的apache
upstream apache_proxy{
ip_hash;
session_sticky;
server localhost:88 max_fails=3 weight=1 fail_timeout=60s;
}
server {
listen 80;
server_name _;
access_log /var/local/nginx/logs/access.log;
index index.html index.htm index.jsp index.php;
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
# 動(dòng)態(tài)頁(yè)面昙啄,交給tomcat處理
location ~ \.(jsp|jspx|do|action)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://tomcat_proxy;
}
# 動(dòng)態(tài)頁(yè)面,交給apache處理
location ~ \.(php)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://apache_proxy;
}
#charset koi8-r;
#access_log logs/host.access.log main;
location /training/ {
proxy_pass http://tomcat_proxy;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#sub_filter /training/ /;
}
# 用戶瀏覽器端的緩存設(shè)置
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 7d;
}
location ~ .*\.(js|css)?$ {
expires 24h;
}
access_log off;
#charset koi8-r;
#access_log logs/host.access.log main;
}
#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;
}
# 包含所有的虛擬主機(jī)的配置文件
include /usr/local/nginx/vhosts/*;
}
4寸五、apache多站點(diǎn)設(shè)置
1.讓Apache在啟動(dòng)時(shí)能加載虛擬主機(jī)模塊梳凛。
打開Apache安裝目錄下conf/httpd.conf文件,找到下面一行文字,把最前面的 # 號(hào)去掉,然后保存梳杏。
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
2.接著找到同一文件中的DocumentRoot和Directory,改為站點(diǎn)目錄的上一級(jí)目錄
例如站點(diǎn)放在 /webapp/example1/www,則改為以下形式
DocumentRoot"/webapp"
<Directory"/webapp">
3.配置完成后文件在最后添加如下:
DocumentRoot是文件放置路徑韧拒,ServerName是網(wǎng)站域名:
<VIRTUALHOST *:88>
ServerAdmin webmaster@example.com
DocumentRoot "/webapp/example1/www " #web目錄路徑
ServerName example.com #host名稱
ServerAlias www.example.com
ErrorLog "/webapp/example1/logs/dummy-host.example.com-error.log"
CustomLog "/webapp/example1/logs/dummy-host.example.com-access.log" common
</virtualhost>
5、tomcat多站點(diǎn)設(shè)置
1 打開tomcat/conf/server.xml十性,在里面找到<Engine name="Catalina" defaultHost="localhost">.....</Engine>
2 在<Engine name="Catalina" defaultHost="localhost"></Engine>中間加入內(nèi)容:
<Engine name="Catalina" defaultHost="localhost">
.........原有內(nèi)容不要?jiǎng)? 下面為新加內(nèi)容:
<Host name="example.com" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="/" crossContext="true" reloadable="true" docBase="/webapp/example1/www" />
</Host>
其中/webapp/exampl
6叛溢、重啟服務(wù)
重啟 Nginx
/etc/init.d/nginx restart
重啟apache
/etc/init.d/httpd restart
重啟tomcat
cd /ilkhome/apache-tomcat-8.0.36/bin/
./shutdown.sh
./startup.sh