您的位置:首页 > 运维架构 > 反向代理

nginx反向代理tomcat、实现动静分离/配置文件

2015-10-28 19:02 976 查看
user nginx nginx;

worker_processes 4;

error_log /usr/local/nginx/logs/nginx_error.log crit;

pid /usr/local/nginx/nginx.pid;

worker_rlimit_nofile 65535;

events {
worker_connections 65535;
use epoll;
}
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;

log_format cache '***$time_local ' '***$upstream_cache_status ' '***Cache-Control: $upstream_http_cache_control '
'***Expires: $upstream_http_expires ' '***"$request" ($status) ' '***"$http_user_agent" ';
access_log logs/cache_access.log cache;

client_max_body_size 200m;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/palin application/x-javascript text/css application/xml;

proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_ignore_client_abort on;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path /home/temp_dir;
proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:1024m inactive=1d max_size=10g;

upstream company {
server 192.168.3.5:8080 ;
server 192.168.3.5:9080 ;
# jvm_route $cookie_JSESSIONID|sessionid reverse;
ip_hash;
}
include /usr/local/nginx/conf/vhost/*.conf;
}
虚拟主机的配置文件
server {
listen 80;
server_name img.sxlfxx.cn;
root /webfile/images;
charset utf-8;
# location / {
# proxy_pass http://company; # proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $remote_addr;
# }

location ~* .*\.(bmp|img|jpeg|jpg|png|gif|svg|png|ico|txt|css|js|html|htm|pdf|apk)$ {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
proxy_cache_valid 200 304 12h;
proxy_cache_valid 301 302 1m;
proxy_cache_valid any 1m;
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Accept-Encoding '';
proxy_ignore_headers "Cache-Control" "Expires";
add_header X-Cache "Hit from ngnix.com";
expires 7d;
}
location ~ .*\.(do|jsp)$ {
proxy_pass http://company; proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_ignore_headers "Cache-Control" "Expires";
add_header X-Cache "Miss from ngnix.com";
}
location /status {
stub_status on;
access_log /usr/local/nginx/logs/status.log;
auth_basic "NginxStatus";
}

}
后端tomcat的虚拟主机配置

<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost" jamRoute="tomcat1">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="a.sxlfxx.cn" appBase="/webfile"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="sxlfxx-a-web" debug="0" reloadable="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<Host name="b.sxlfxx.cn" appBase="/webfile"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="sxlfxx-b-web" debug="0" reloadable="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<Host name="c.sxlfxx.cn" appBase="/webfile"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="sxlfxx-c-web" debug="0" reloadable="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
<Host name="www.sxlfxx.cn" appBase="/webfile"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="sxlfxx-www-web" debug="0" reloadable="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
</Host>
</Engine>
</Service>
</Server>

mysql主配置文件:

[client]

port = 31633
socket = /service/data/mysqld.sock

[mysqld]
port = 31633
socket = /service/data/mysqld.sock
datadir = /service/data/

long_query_time = 3
skip-name-resolve
expire_logs_days = 10

skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M

log-bin=mysqlmaster-bin.log
sync_binlog=1
innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
log_bin_trust_function_creators=1
event_scheduler=ON

server-id = 1

[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
mysql从数据库:
[client]
port = 31633
socket = /service/data/mysqld.sock
[mysqld]
port = 31633
socket = /service/data/mysqld.sock
datadir = /service/data/
long_query_time = 3
skip-name-resolve
expire_logs_days = 10
lower_case_table_names= 1
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 1000M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
log-bin=mysqlslave-bin.log
sync_binlog=1
innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
log_bin_trust_function_creators=1
binlog_format=mixed
innodb-support_xa = 1
read_only
open_files=5000
server-id = 2
slave_skip_errors = 1062
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
#innodb_force_recovery= 1
#skip_slave_start
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

mysql主从配置笔记:
ySQL数据库软件安装完成后,不要急着做mysql启动操作。建议把mysql初始化生成的/usr/my.cnf
(如果是从源文件编译安装时,路径应该是在/usr/local/mysql/mysql.cnf)删除,然后把优化好的mysql
配置文件my.cnf放到/etc下。
第三步:修改主数据库的配置文件/usr/my.cnf
[mysqld]
server-id=1
log-bin=mysqlmaster-bin.log
sync_binlog=1
innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
log_bin_trust_function_creators=1
第四步:修改从数据库配置文件/usr/my.cnf
server-id=2
log-bin=mysqlslave-bin.log
sync_binlog=1
innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
log_bin_trust_function_creators=1
第五步:在主数据库和从数据库服务器上分别执行以下命令重新启动主数据库和从数据库
[root@master ~]# service mysql restart
[root@slave ~]# service mysql restart
第六步:在主数据库上创建用于主从复制的账户
[root@master ~]# mysql -uroot -p
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.165.113.17' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
第七步:主数据库锁表(禁止再插入数据以获取主数据库的的二进制日志坐标)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
第八步:查看主数据库的状态(并记录下File字段和Position字段的值,在配置从服务器时有用到)
mysql> show master status;
+------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------+----------+--------------+------------------+-------------------+
| mysqlmaster-bin.000004 | 327 | | | |
+------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
第九步:创建主数据库的快照文件
[root@master ~]# cd /usr/bin/
# ./mysqldump -uroot -p -h127.0.0.1 -P31633 --all-databases --triggers --routines --events >>/all.sql
第十步:解锁主数据库的锁表操作
[root@master ~]# mysql -uroot -p (本命令在主数据库服务器上执行)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
第十一步:在从数据库服务器上导入第七步创建的快照文件到从数据库中
[root@slave ~]# mysql -uroot -p -h127.0.0.1 -P31633 < /mnt/windows/all.sql
第十二步:在从数据库服务器上设置主数据库服务器向从数据库服务器同步
[root@slave ~]# mysql -uroot -p
mysql> change master to master_host = '10.171.81.7',master_user='repl',master_password='123456',master_port=31633,master_log_file='mysqlmaster-bin.000001',master_log_pos=330;
注意:红色部分的值,是在第八步中查出来的,这里不能弄错了
第十三步:启动从数据库复制线程
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
第十四步:查询从数据库的复制线程状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.100
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysqlmaster-bin.000004
Read_Master_Log_Pos: 327
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 289
Relay_Master_Log_File: mysqlmaster-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 327
Relay_Log_Space: 462
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 2e5e1b22-f0a9-11e3-bbac-000c297799e0
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)

本文出自 “→おyǒňɡ無祉境℃” 博客,转载请与作者联系!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: