Nginx reverse proxy tomcat for static/dynamic separation/configuration file
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;}
Configuration file of the VM
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";}}
Backend tomcat Virtual Host Configuration
<!-- 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 master configuration file:
[client]port = 31633socket = /service/data/mysqld.sock[mysqld]port = 31633socket = /service/data/mysqld.sockdatadir = /service/data/long_query_time = 3skip-name-resolveexpire_logs_days = 10skip-external-lockingkey_buffer_size = 384Mmax_allowed_packet = 1Mtable_open_cache = 512sort_buffer_size = 2Mread_buffer_size = 2Mread_rnd_buffer_size = 8Mmyisam_sort_buffer_size = 64Mthread_cache_size = 8query_cache_size = 32Mlog-bin=mysqlmaster-bin.logsync_binlog=1innodb_buffer_pool_size=512Minnodb_flush_log_at_trx_commit=1sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLESlower_case_table_names=1log_bin_trust_function_creators=1event_scheduler=ONserver-id = 1[mysqldump]quickmax_allowed_packet = 16M[mysql]no-auto-rehash[myisamchk]key_buffer_size = 256Msort_buffer_size = 256Mread_buffer = 2Mwrite_buffer = 2M[mysqlhotcopy]interactive-timeout
Mysql slave database:
[client]port = 31633socket = /service/data/mysqld.sock[mysqld]port = 31633socket = /service/data/mysqld.sockdatadir = /service/data/long_query_time = 3skip-name-resolveexpire_logs_days = 10lower_case_table_names= 1skip-external-lockingkey_buffer_size = 384Mmax_allowed_packet = 1000Mtable_open_cache = 512sort_buffer_size = 2Mread_buffer_size = 2Mread_rnd_buffer_size = 8Mmyisam_sort_buffer_size = 64Mthread_cache_size = 8query_cache_size = 32Mlog-bin=mysqlslave-bin.logsync_binlog=1innodb_buffer_pool_size=512Minnodb_flush_log_at_trx_commit=1sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLESlower_case_table_names=1log_bin_trust_function_creators=1binlog_format=mixedinnodb-support_xa = 1read_onlyopen_files=5000server-id = 2slave_skip_errors = 1062[mysqldump]quickmax_allowed_packet = 16M[mysql]no-auto-rehash#innodb_force_recovery= 1#skip_slave_start[myisamchk]key_buffer_size = 256Msort_buffer_size = 256Mread_buffer = 2Mwrite_buffer = 2M[mysqlhotcopy]interactive-timeout
Mysql Master/Slave configuration notes: Do not rush to start mysql after the ySQL database software is installed. We recommend that you initialize/usr/my. cnf (if it is compiled and installed from the source file, the path should be in/usr/local/mysql. cnf), and then the optimized mysql configuration file my. put cnf under/etc. Step 3: modify the configuration file/usr/my. cnf of the primary database
[mysqld]server-id=1log-bin=mysqlmaster-bin.logsync_binlog=1innodb_buffer_pool_size=512Minnodb_flush_log_at_trx_commit=1sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLESlower_case_table_names=1log_bin_trust_function_creators=1
Step 4: Modify the slave Database Configuration File/usr/my. cnf
server-id=2log-bin=mysqlslave-bin.logsync_binlog=1innodb_buffer_pool_size=512Minnodb_flush_log_at_trx_commit=1sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLESlower_case_table_names=1log_bin_trust_function_creators=1
Step 5: run the following commands on the master database and slave database server to restart the master database and slave database [root @ master ~] respectively. # Service mysql restart [root @ slave ~] # Service mysql restart Step 6: create an account for master-slave replication on the master database [root @ master ~] # Mysql-uroot-pmysql> grant replication slave on *. * TO 'repl' @ '10. 165.113.17 'identified BY '123'; Query OK, 0 rows affected (123456 sec) Step 7: master database lock table (disable data insertion to obtain the binary log coordinates of the master database) mysql> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) Step 8: view the status of the primary database (and record the values of the File field and Position field, used When configuring slave servers) mysql> show master status; + ------------------------ + ---------- + -------------- + ---------------------- + ------ ------------- + | File | Position | Binlog_Do_DB | bytes | + bytes + ---------- + -------------- + bytes + | mysqlmaster-bin.000004 | 327 | + bytes + ---------- + -------------- + ------------------ + ------------------- + 1 row in set (0.00 sec) step 9: Create a snapshot file for the primary database [root @ master ~] # Cd/usr/bin /#. /mysqldump-uroot-p-h127.0.0.1-p000033 -- all-databases -- triggers -- routines -- events>/all. step 10 of SQL: Unlock the lock table of the primary database [root @ master ~] # Mysql-uroot-p (this command is executed on the master database server) mysql> unlock tables; Query OK, 0 rows affected (0.00 sec) Step 1: import the snapshot File Created in Step 7 from the database server to the slave database [root @ slave ~] # Mysql-uroot-p-h127.0.0.1-p000033 </mnt/windows/all. step 2 of SQL: Set the master database server on the slave database server to synchronize [root @ slave ~] to the slave Database Server # Mysql-uroot-pmysql> change master to master_host = '10. 171.81.7 ', master_user = 'repl', master_password = '000000', master_port = 123456, master_log_file = 'mysqlmaster-bin.000001', master_log_pos = 31633; Note: Values in red, step 8: start the replication thread from the database mysql> start slave; Query OK, 0 rows affected (13th sec) Step 2: query the replication thread status of the slave Database
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: 0Master_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: 01 row in set (0.00 sec)