1. 程式人生 > >nginx + mysql 配置服務端的配置

nginx + mysql 配置服務端的配置

#雙核CPU
user  nobody;
worker_processes 4 ;
worker_cpu_affinity 0001 0010 0100 1000 ;
worker_rlimit_nofile 65535;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    use   epoll;             #epoll是多路複用IO(I/O Multiplexing)中的一種方式,但是僅用於linux2.6以上核心,可以大大提高nginx的效能	
    multi_accept on;
    worker_connections 2048 ;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    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  /var/log/nginx/access.log  main;
    #tcp_nopush     on;
    tcp_nopush on;
    tcp_nodelay on;
    access_log off;
    client_header_timeout 10;
    client_body_timeout 10;
    reset_timedout_connection on;
    send_timeout 10;
   

    keepalive_timeout  65;#超時時間
    client_header_buffer_size 4k;#客戶端請求頭部的緩衝區大小,這個可以根據你的系統分頁大小來設定
    open_file_cache max=102400 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;

    gzip on;
    gzip_disable "msie6";
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 4;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
   
    #壓縮型別,預設就已經包含text/html,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn。
    
	
   
   
     include /etc/nginx/conf.d/*.conf;
}
server {
    listen       80;
    server_name  wx.lylife.com.cn lylife.com.cn;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /www/www/we7new;
        index  index.html index.htm index.php;
        autoindex off;
    }

    #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   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
   location ~* ^/(attachment|static|images)/.*.(php|php5)$ {
    	deny all;
   }
	location ~* ^/addons/ewei_shopv2/(static|images|data|cert)/.*.(php|php5)$ {
       		deny all;
 	  }
     location ~ \.php$ {
            root           /www/www/we7new;
            fastcgi_pass   127.0.0.1:9000;
	    #fastcgi_pass unix:/dev/shm/fpm-cgi.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
     {
        expires       15d;
     }
     location ~ .*\.(js|css)?$
     {
        expires       1d;
     }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

Myssql5.7 配置

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
slow_query_log=ON
long_query_time=1
slow_query_log_file = /var/log/mysql/mysql-slow.log

general_log_file=/var/log/mysql/mysql-general-log.log
general_log = OFF
#避免Mysql的外部鎖定,減少出錯機率,增強穩定性:
skip-external-locking
#如果是1,那麼查詢總是先到查詢快取中查詢,即使使用了sql_no_cache仍然查詢快取,因為sql_no_cache只是不快取查詢結果,而不是不使用查詢結果。
query_cache_type=1
#back_log引數的值指出在MySQL暫時停止響應新請求之前,短時間內的多少個請求可以被存,在對堆疊中,如果系統短時間內有很多連線,則需>要增大該引數的值,該引數值指定到來的TCP/IP連線的監聽佇列的大小。不同的作業系統在這個佇列的大小有自己的限制,將back_log設定得高於作業系統的限制將是無效的,其預設值為50,對於LINUX系統而言,推薦設定為小於512的整數:
back_log=100
binlog_cache_size=64M
#索引緩衝區大小,增加它可得到更好的索引處理效能,對於記憶體在4GB左右的伺服器,該引數可設定為256M或384M。如果該引數值設定的過大>反而會使伺服器的整體效率降低:
key_buffer_size=512M
#設定在網路傳輸中一次訊息傳輸量的最大值,系統預設值為1MB,最大值是1GB,必須設定為1024的倍數,單位為位元組:
max_allowed_packet=64M
#設定MySQL每個執行緒的堆疊大小,預設值足夠大,可滿足普通操作。可設定範圍為128KB至4GB,預設192K:
thread_stack=64M
sort_buffer_size=64M
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache=256
max_heap_table_size=16M
#設定Thread Cache池中可以快取的連線池執行緒最大數量,可設定為0-16384,預設為0。1GB記憶體我們配置為8,2GB記憶體我們配置為16,4GB或4GB以上內在我們配置為64:
thread_cache_size=64
#指定Mysql查詢緩衝區的大小,可以通過在Mysql控制檯觀察,如果Qcache_lowmem_prunes的值非常大,則表明經常出現緩衝不夠的情況,如果 Qcache_hits的值非常大,則表明查詢緩衝使用的非常頻繁 :
query_cache_size=64M
query_cache_limit=64M
#設定內在臨時表最大值,如果超過該值,則會將臨時表寫入磁碟,其範圍為1KB至4GB:
tmp_table_size=64M
#指定一個請求的最大連線時間,對於4GB左右內在的伺服器來說,可以將其設定為5-10:
wait_timeout=20
#開啟該選項可以徹底關閉MYSQL的TCP/IP連線方式,如果WEB伺服器是以遠端連線的方式訪問MYSQL的資料庫伺服器,則不要開啟該選項,否則>將無法正常連線。 skip-networking   抱怨Innodb比MyISAM慢 100倍?那麼你大概是忘了調整這個值。預設值1的意思是每一次事務提交或事務外的指令都需要把日誌寫入(flush) 硬碟,這是很費時的。特別是使用電池供電快取(Battery backed up cache)時。設成2對於很多運用,特別是從MyISAM錶轉過來的是可以的>,它的意思是不寫入硬碟而是寫入系統快取。日誌仍然會每秒flush到硬 盤,所以你一般不會丟失超過1-2秒的更新。設成0會更快一點,但安>全方面比較差,即使MySQL掛了也可能會丟失事務的資料。而值2只會在整個操作系#統 掛了時才可能丟資料:
innodb_flush_log_at_trx_commit=2
#這是 InnoDB 儲存引擎的事務日誌所使用的緩衝區。類似於 Binlog Buffer,InnoDB 在寫事務日誌的時候,為了提高效能,也是先將資訊寫>入 Innofb Log Buffer 中,當滿足 innodb_flush_log_trx_commit 引數所設定的相應條件(或者日誌緩衝區寫滿)之後,才會將日誌寫到文>件(或者同步到磁碟)中。可以通過 innodb_log_buffer_size 引數設定其可以使用的最大記憶體空間:
innodb_log_buffer_size=64M

innodb_buffer_pool_size=64M

innodb_log_file_size=1G

innodb_file_per_table=1

innodb_read_io_threads=10

innodb_write_io_threads=10

innodb_flush_method=O_DIRECT

innodb_io_capacity=1000

innodb_io_capacity_max=1000

innodb_lru_scan_depth=500

innodb_thread_concurrency=0  

innodb_autoinc_lock_mode=2

innodb_log_files_in_group=3

innodb_max_dirty_pages_pct=90

innodb_lock_wait_timeout=100

bulk_insert_buffer_size = 16M  

innodb_thread_concurrency = 8
innodb_purge_threads = 1
#tmp_table_size 的預設大小是 32M。如果一張臨時表超出該大小,MySQL產生一個 The table tbl_name is full 形式的錯誤,如果你做很多 #高階 GROUP BY 查詢,增加 tmp_table_size 值:
tmp_table_size=512M
#隨機讀取資料緩衝區使用記憶體(read_rnd_buffer_size):和順序讀取相對應,當 MySQL 進行非順序讀取(隨機讀取)資料塊的時候,會利用>這個緩衝區暫存讀取的資料。如根據索引資訊讀取表資料,根據排序後的結果集與表進行Join等等。總的來說,就是當資料塊的讀取需要滿足>一定的順序的情況下,MySQL 就需要產生隨機讀取,進而使用到 read_rnd_buffer_size 引數所設定的記憶體緩衝區:
read_rnd_buffer_size=128M
#預設配置沒開查詢快取:
explicit_defaults_for_timestamp
#需要記錄進位制日誌的資料庫.如果有多個數據庫可用逗號分隔,或者使用多個binlog-do-db選項:
binlog-do-db=dbname
#不需要記錄進位制日誌的資料庫.如果有多個數據庫可用逗號分隔,或者使用多個binlog-do-db選項  :
binlog-ignore-db=dbname
#過濾掉一些沒什麼大問題的錯誤:
slave-skip-errors=all
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[mysqldump]
host=localhost
user=mysqlback
password='password'