nginx 基本配置都是在安裝目錄下的 conf/nginx.conf 文件中。
看下面的默認配置:
http {
include mime.types; // 可以單獨配置文件
}
MIME-type 和 Content-Type 的關系: 當 web 服務器收到靜態的資源文件請求時,依據請求文件的后綴名在服務器的 MIME 配置文件中找到對應的 MIME Type,再根據 MIME Type 設置 HTTP Response 的Content-Type,然后瀏覽器根據 Content-Type 的值處理文件。
什么是 MIME-TYPE: 為什么這么說呢?首先,我們要了解瀏覽器是如何處理內容的。在瀏覽器中顯示的內容有 HTML、有 XML、有 GIF、還有 Flash ... 那么,瀏覽器是如何區分它們,絕定什么內容用什么形式來顯示呢?答案是 MIME Type,也就是該資源的媒體類型。
媒體類型通常是通過 HTTP 協議,由 Web 服務器告知瀏覽器的,更準確地說,是通過 Content-Type 來表示的,例如: Content-Type: text/HTML 表示內容是 text/HTML 類型,也就是超文本文件。為什么是 “text/HTML” 而不是 “HTML/text” 或者別的什么?MIME Type 不是個人指定的,是經過 ietf 組織協商,以 RFC 的形式作為建議的標準發布在網上的,大多數的 Web 服務器和用戶代理都會支持這個規范 (順便說一句,Email 附件的類型也是通過 MIME Type 指定的)。 通常只有一些在互聯網上獲得廣泛應用的格式才會獲得一個 MIME Type,如果是某個客戶端自己定義的格式,一般只能以 application/x- 開頭。 XHTML 正是一個獲得廣泛應用的格式,因此,在 RFC 3236 中,說明了 XHTML 格式文件的 MIME Type 應該是 application/xHTML+XML。 當然,處理本地的文件,在沒有人告訴瀏覽器某個文件的 MIME Type 的情況下,瀏覽器也會做一些默認的處理,這可能和你在操作系統中給文件配置的 MIME Type 有關。比如在 Windows 下,打開注冊表的“HKEY_LOCAL_MACHINESOFTWAREClassesMIMEDatabaseContent Type”主鍵,你可以看到所有 MIME Type 的配置信息。
配置站點自然是在 nginx.conf 文件內,但是,不利于維護,所以利用 include 在別處創建一個文件夾,專門收集站點的配置;比如我有兩個站點需要配置,在 conf 文件夾下新建一個 servers 由于放置站點的配置;并增加 test.conf 以及 test2.conf,然后在 nginx.conf 文件內,新增配置:他會讀取所有 server 文件夾下的 conf 結尾的配置。
http {
include mime.types;
include servers/*.conf;
}
test.conf 中的內容:
# 表示要啟動一個服務
server {
listen 9999; # 服務監聽的端口
server_name localhost; # 瀏覽器訪問的 host name
# 要代理的路由
location / {
# 匹配到 localhost:9999/ 代理到下面的地址
proxy_pass http://localhost:8888;
proxy_set_header Host $host;
}
# 匹配到 localhost:9999/api/ 代理到下面的地址
location /api/ {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
}
error_page 500 502 503 504 /50x.html;
location=/50x.html {
root html;
}
}
注意這里的 proxy_set_header Host $host; 他的意思是設置 nginx 發送代理請求時候請求的 host 設置為瀏覽器請求的 host 而不是 nginx 默認設置的 proxy_pass 的 host,比如我們配置的 proxy_pass http://localhost:8888; 如果在服務器端獲取 host,是獲取到 127.0.0.1:8888,而服務器端想要的是真正的瀏覽器發送請求的 host,可以設置 proxy_set_header Host $host; $host 是 nginx 中的一個變量,他是每個請求帶過來的 host。
在 test.conf 添加下面的配置:
# 新建緩存配置項
proxy_cache_path cache levels=1:2 keys_zone=my_cache:10m;
# 表示要啟動一個服務
server {
listen 9999; # 服務監聽的端口
server_name localhost; # 瀏覽器訪問的 host name
# 要代理的路由
location / {
# 匹配到 localhost:9999/ 代理到下面的地址
proxy_pass http://localhost:8888;
# 使用緩存配置
proxy_cache my_cache;
}
}
proxy_cache_path cache levels=1:2 keys_zone=my_cache:10m
首先 proxy_cache_path 是緩存選項的配置,cache 是緩存文件的存放路徑(相對路徑);levels 是否要創建二級文件夾,因為 cache 可以用在不同的站點緩存,如果都存放在 cache 文件夾中,沒有子目錄,隨著緩存的越來越多,查找緩存的速度也會越來越慢;所以要新建一個二級文件夾。keys_zone 存儲大小以及存儲的緩存的名字。
新建一個 node 服務:
const http=require('http')
const fs=require('fs')
const wait=(second)=> {
return new Promise((resolve, reject)=> {
setTimeout(()=> {
resolve()
}, second * 1000)
})
}
http.createServer(function(req, res) {
if (req.url==='/') {
const html=fs.readFileSync('test.html', 'utf-8')
res.writeHead(200, {
'Content-Type': 'text/html'
})
res.end(html)
}
if (req.url==='/data') {
res.writeHead(200, {
'Cache-Control': 'max-age=20'
})
wait(2).then(()=> res.end('success'))
}
}).listen(8888)
我們對 '/data' 接口進行了緩存設置 'Cache-Control': 'max-age=20' ,然后使用 nginx 代理服務代理接口:
proxy_cache_path cache levels=1:2 keys_zone=my_cache:10m;
# 表示要啟動一個服務
server {
listen 9999; # 服務監聽的端口
server_name localhost; # 瀏覽器訪問的 host name
# 要代理的路由
location / {
proxy_cache my_cache;
# 匹配到 localhost:9999/ 代理到下面的地址
proxy_pass http://localhost:8888;
}
然后第一次請求時候,會等待兩秒顯示 success ,如果下次請求,代理服務器已經對數據進行了緩存,直接會顯示出來,這時候打開其他瀏覽器,之前并沒有訪問過這個地址, success 也會直接出來。說明 代理服務器緩存配置生效了;
可以看到只設置了 max-age=2 為兩秒,但是兩秒后刷新頁面還是請求很快,是因為 s-maxage=20 只有在代理服務器里面才會生效;也就是說瀏覽器還是會讀取 max-age 作為緩存的到期時間。如果在代理服務器中如果同時設置了 max-age 和 s-maxage;他會讀取 s-maxage,因為這個 s-maxage 配置項是專門為代理服務器設置的。
const http=require('http')
const fs=require('fs')
const wait=(second)=> {
return new Promise((resolve, reject)=> {
setTimeout(()=> {
resolve()
}, second * 1000)
})
}
http.createServer(function(req, res) {
if (req.url==='/') {
const html=fs.readFileSync('test.html', 'utf-8')
res.writeHead(200, {
'Content-Type': 'text/html'
})
res.end(html)
}
if (req.url==='/data') {
res.writeHead(200, {
'Cache-Control': 'max-age=2; s-maxage=20; private'
})
wait(2).then(()=> res.end('success'))
}
}).listen(8888)
private:http 請求返回的過程中設置 Cache-Control 為 private,代表發起請求的瀏覽器可以進行緩存。實際操作中可以會體會到5秒以后,重新進行請求,還是等待兩秒顯示 success;
const http=require('http')
const fs=require('fs')
const wait=(second)=> {
return new Promise((resolve, reject)=> {
setTimeout(()=> {
resolve()
}, second * 1000)
})
}
http.createServer(function(req, res) {
if (req.url==='/') {
const html=fs.readFileSync('test.html', 'utf-8')
res.writeHead(200, {
'Content-Type': 'text/html'
})
res.end(html)
}
if (req.url==='/data') {
res.writeHead(200, {
'Cache-Control': 'max-age=5; s-maxage=20; private'
})
wait(2).then(()=> res.end('success'))
}
}).listen(8888)
vary 是判斷如果是一樣的頭信息,就進行返回緩存的結果。 使用場景:我們使用了服務器端緩存,我們通過 User-Agent 進行返回緩存的數據;如果頭信息相同就進行返回緩存結果。或者 Content-language 根據不同的語言返回緩存結果。 下面例子中,如果之前加載過 X-Test-Cache : 1,下次加載的時候會直接讀取緩存。 我們就可以限制不僅 url 相同,如果頭信息相同才會進行緩存。如果指定的頭信息不一樣,不使用緩存。
MySQL 是最流行的關系型數據庫管理系統,由瑞典MySQL AB公司開發,目前屬于Oracle公司。
MySQL所使用的SQL語言是用于訪問數據庫的最常用標準化語言。
MySQL由于其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,一般中小型網站的開發都選擇MySQL作為網站數據庫。
MySQL 安裝
本教程的系統平臺:CentOS release 6.6 (Final) 64位。
一、安裝編譯工具及庫文件
yum -y install gcc gcc-c++ make autoconf libtool-ltdl-devel gd-devel freetype-devel libxml2-devel libjpeg-devel libpng-devel openssl-devel curl-devel bison patch unzip libmcrypt-devel libmhash-devel ncurses-devel sudo bzip2 flex libaio-devel
二、 安裝cmake 編譯器
cmake 版本:cmake-3.1.1。
1、下載地址:http://www.cmake.org/files/v3.1/cmake-3.1.1.tar.gz
$ wget http://www.cmake.org/files/v3.1/cmake-3.1.1.tar.gz
2、解壓安裝包
$ tar zxvf cmake-3.1.1.tar.gz
3、進入安裝包目錄
$ cd cmake-3.1.1
4、編譯安裝
$ ./bootstrap $ make && make install
三、安裝 MySQL
MySQL版本:mysql-5.6.15。
1、下載地址: http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.15.tar.gz
$ wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.15.tar.gz
2、解壓安裝包
$ tar zxvf mysql-5.6.15.tar.gz
3、進入安裝包目錄
$ cd mysql-5.6.15
4、編譯安裝
$ cmake -DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql/ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_INNODB_MEMCACHED=1 -DWITH_DEBUG=OFF -DWITH_ZLIB=bundled -DENABLED_LOCAL_INFILE=1 -DENABLED_PROFILING=ON -DMYSQL_MAINTAINER_MODE=OFF -DMYSQL_DATADIR=/usr/local/webserver/mysql/data -DMYSQL_TCP_PORT=3306$ make && make install
5、查看mysql版本:
$ /usr/local/webserver/mysql/bin/mysql --version
到此,mysql安裝完成。
MySQL 配置
1、創建mysql運行使用的用戶mysql:
$ /usr/sbin/groupadd mysql $ /usr/sbin/useradd -g mysql mysql
2、創建binlog和庫的存儲路徑并賦予mysql用戶權限
$ mkdir -p /usr/local/webserver/mysql/binlog /www/data_mysql $ chown mysql.mysql /usr/local/webserver/mysql/binlog/ /www/data_mysql/
3、創建my.cnf配置文件
將/etc/my.cnf替換為下面內容
$ cat /etc/my.cnf[client]port=3306socket=/tmp/mysql.sock[mysqld]replicate-ignore-db=mysql replicate-ignore-db=test replicate-ignore-db=information_schema user=mysql port=3306socket=/tmp/mysql.sock basedir=/usr/local/webserver/mysql datadir=/www/data_mysql log-error=/usr/local/webserver/mysql/mysql_error.log pid-file=/usr/local/webserver/mysql/mysql.pid open_files_limit=65535back_log=600max_connections=5000max_connect_errors=1000table_open_cache=1024external-locking=FALSE max_allowed_packet=32Msort_buffer_size=1Mjoin_buffer_size=1Mthread_cache_size=600#thread_concurrency=8query_cache_size=128Mquery_cache_limit=2Mquery_cache_min_res_unit=2kdefault-storage-engine=MyISAMdefault-tmp-storage-engine=MYISAM thread_stack=192Ktransaction_isolation=READ-COMMITTED tmp_table_size=128Mmax_heap_table_size=128Mlog-slave-updates log-bin=/usr/local/webserver/mysql/binlog/binlog binlog-do-db=oa_fb binlog-ignore-db=mysql binlog_cache_size=4Mbinlog_format=MIXED max_binlog_cache_size=8Mmax_binlog_size=1Grelay-log-index=/usr/local/webserver/mysql/relaylog/relaylog relay-log-info-file=/usr/local/webserver/mysql/relaylog/relaylog relay-log=/usr/local/webserver/mysql/relaylog/relaylog expire_logs_days=10key_buffer_size=256Mread_buffer_size=1Mread_rnd_buffer_size=16Mbulk_insert_buffer_size=64Mmyisam_sort_buffer_size=128Mmyisam_max_sort_file_size=10Gmyisam_repair_threads=1myisam_recover interactive_timeout=120wait_timeout=120skip-name-resolve#master-connect-retry=10slave-skip-errors=1032,1062,126,1114,1146,1048,1396#master-host=192.168.1.2#master-user=username#master-password=password#master-port=3306server-id=1loose-innodb-trx=0 loose-innodb-locks=0 loose-innodb-lock-waits=0 loose-innodb-cmp=0 loose-innodb-cmp-per-index=0loose-innodb-cmp-per-index-reset=0loose-innodb-cmp-reset=0 loose-innodb-cmpmem=0 loose-innodb-cmpmem-reset=0 loose-innodb-buffer-page=0 loose-innodb-buffer-page-lru=0 loose-innodb-buffer-pool-stats=0 loose-innodb-metrics=0 loose-innodb-ft-default-stopword=0 loose-innodb-ft-inserted=0 loose-innodb-ft-deleted=0 loose-innodb-ft-being-deleted=0 loose-innodb-ft-config=0 loose-innodb-ft-index-cache=0 loose-innodb-ft-index-table=0 loose-innodb-sys-tables=0 loose-innodb-sys-tablestats=0 loose-innodb-sys-indexes=0 loose-innodb-sys-columns=0 loose-innodb-sys-fields=0 loose-innodb-sys-foreign=0 loose-innodb-sys-foreign-cols=0slow_query_log_file=/usr/local/webserver/mysql/mysql_slow.log long_query_time=1[mysqldump]quick max_allowed_packet=32M
4、初始化數據庫
$/usr/local/webserver/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql
顯示如下信息:
Installing MySQL system tables...2015-01-26 20:18:51 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).OKFilling help tables...2015-01-26 20:18:57 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).OK...
5、創建開機啟動腳本
$ cd /usr/local/webserver/mysql/$ cp support-files/mysql.server /etc/rc.d/init.d/mysqld $ chkconfig --add mysqld $ chkconfig --level 35 mysqld on
6、啟動mysql服務器
$ service mysqld start
7、連接 MySQL
$ /usr/local/webserver/mysql/bin/mysql -u root -p
修改MySQL用戶密碼
mysqladmin -u用戶名 -p舊密碼 password 新密碼
或進入mysql命令行
SET PASSWORD FOR '用戶名'@'主機'=PASSWORD(‘密碼');
創建新用戶并授權:
grant all privileges on *.* to 用戶名@'%' identified by '密碼' with grant option;
其他命令
啟動:service mysqld start
停止:service mysqld stop
重啟:service mysqld restart
重載配置:service mysqld reload