先说下我的需求:
我在一台机器上部署了两个环境test和uat,希望通过两个域名访问,在另外一台机器上部署了nginx,通过一个nginx实现访问一个服务器上两个域名网站,具体配置如下:
另外配置还实现了动静分离。
1、nginx.conf 配置
#user nobody;
#处理进程数量
worker_processes 2;
#pid logs/nginx.pid;
worker_rlimit_nofile 51200;
events {
use epoll;
worker_connections 51200;
}
http {
include mime.types;
default_type application/octet-stream;
#nginx的HttpLog模块指定,指定nginx日志的输出格式,输出格式为access
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /opt/fmsj/nginx-pro/log/access.log access;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 356m;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
server_tokens off;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_send_timeout 60;
proxy_read_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
#开启gzip
gzip on;
#允许压缩的最小字节数
gzip_min_length 1k;
#4个单位为16k的内存作为压缩结果流缓存
gzip_buffers 4 16k;
#设置识别HTTP协议版本,默认是1.1
gzip_http_version 1.1;
#gzip压缩比,可在1~9中设置,1压缩比最小,速度最快,9压缩比最大,速度最慢,消耗CPU
gzip_comp_level 3;
#压缩的类型
gzip_types text/plain text/css application/x-javascript image/bmp application/javascript;
#让前端的缓存服务器混村经过的gzip压缩的页面
gzip_vary on;
#在指定的目录下面配置多个,server
include /opt/fmsj/nginx-pro/conf/myconf/*.conf;
}
2、配置test.zjs.com 和 uat.zjs.com
test.zjs.conf文件配置如下:
server {
listen 80;
server_name test.zjs.com;
charset utf-8;
#设定请求缓冲
#server_names_hash_bucket_size 128;
root /opt/fmsj/apache-tomcat-8.0.21-test/webapps/fmsj/;
location ~ \.(jsp|do)?$
{
proxy_pass http://10.44.213.85:9090;
root /opt/fmsj/apache-tomcat-8.0.21-test/webapps/fmsj/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $remote_addr;
index index.jsp;
}
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma|eot|svg|ttf|woff|woff2)$ #由nginx处理静态页面
{
expires 30d; #使用expires缓存模块,缓存到客户端30天
root /opt/fmsj/apache-tomcat-8.0.21-test/webapps/;
}
location ~ .*\.(js|css)?$
{
expires 1d;
root /opt/fmsj/apache-tomcat-8.0.21-test/webapps/;
}
location / {
root /opt/fmsj/apache-tomcat-8.0.21-test/webapps/;
index index.jsp;
proxy_pass http://10.44.213.85:9090;
}
#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;
}
}
uat.zjs.com 文件如下:
server {
listen 80;
server_name uat.zjs.com;
charset utf-8;
#设定请求缓冲
#server_names_hash_bucket_size 128;
root /opt/fmsj/apache-tomcat-8.0.21-uat/webapps/fmsj/;
location ~ \.(jsp|do)?$
{
proxy_pass http://10.44.213.85:9898;
root /opt/fmsj/apache-tomcat-8.0.21-uat/webapps/fmsj/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $remote_addr;
index index.jsp;
}
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma|eot|svg|ttf|woff|woff2)$ #由nginx处理静态页面
{
expires 30d; #使用expires缓存模块,缓存到客户端30天
root /opt/fmsj/apache-tomcat-8.0.21-uat/webapps/;
}
location ~ .*\.(js|css)?$
{
expires 1d;
root /opt/fmsj/apache-tomcat-8.0.21-uat/webapps/;
}
location / {
root /opt/fmsj/apache-tomcat-8.0.21-uat/webapps/;
index index.jsp;
proxy_pass http://10.44.213.85:9898;
}
#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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}