前言
Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。由俄羅斯的程序設計師Igor Sysoev所開發,供俄國大型的入口網站及搜索引擎Rambler(俄文:Рамблер)使用。其特點是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好。(百度百科)
1.Nginx安裝
我使用的環境是64位 Ubuntu 14.04。nginx依賴以下模塊:
- gzip模塊需要 zlib 庫
- rewrite模塊需要 pcre 庫
- ssl 功能需要openssl庫
1.1.安裝pcre
1.2.安裝openssl
1.3.安裝zlib
1.4.安裝nginx
下載pcre、openssl、zlib、及其nginx安裝包 下載地址:http://download.csdn.net/download/liu976180578/10134160- 獲取pcre編譯安裝包 下載地址:http://download.csdn.net/download/liu976180578/10134160
- 解壓縮pcre-xx.tar.gz包。
- 進入解壓縮目錄,執行./configure。
- make & make install
- 獲取openssl編譯安裝包 下載地址:http://download.csdn.net/download/liu976180578/10134160
- 解壓縮openssl-xx.tar.gz包。
- 進入解壓縮目錄,執行./config。
- make & make install
- 獲取zlib編譯安裝包 下載地址:http://download.csdn.net/download/liu976180578/10134160
- 解壓縮openssl-xx.tar.gz包。
- 進入解壓縮目錄,執行./configure。
- make & make install
- 獲取nginx
- 解壓縮nginx-xx.tar.gz包。 下載地址:http://download.csdn.net/download/liu976180578/10134160
- 進入解壓縮目錄,執行./configure
- make & make install
安裝nginx注意事項
若安裝時找不到上述依賴模塊,使用--with-openssl=<openssl_dir>、--with-pcre=<pcre_dir>、--with-zlib=<zlib_dir>指定依賴的模塊目錄。
nginx解壓包下命令
./configure --with-openssl=<openssl_dir> --with-pcre=<pcre_dir> --with-zlib=<zlib_dir>
啓動nginx
安裝成功後,系統默然會把nginx放在/usr/local/nginx 目錄下
進入/usr/local/nginx 目錄, 執行啓動命令 sbin/nginx
重啓nginx
必須cd 到sbin目錄,必須是在已啓動時執行
cd /usr/local/nginx/sbin sudo
./nginx -s reload
啓動nginx之後,瀏覽器中輸入http://localhost可以驗證是否安裝啓動成功。
Nginx的簡單配置
- #Nginx創建進程所用用戶和用戶組
- #user nobody;
- user test test;
- #進程數,一般等於CPU數量
- worker_processes 4;
- #錯誤日誌,定義到warn級別比較合適
- #error_log logs/error.log;
- error_log logs/error.log warn;
- #指定pid存放文件
- pid logs/nginx.pid;
- events {
- #使用網絡IO模型linux建議epoll,FreeBSD建議採用kqueue
- use epoll;
- #允許最大連接數
- worker_connections 1024;
- }
- 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"';
- #關閉日誌能夠減少IO,但是建議開啓error_log warn級別
- #access_log logs/access.log main;
- sendfile on;
- #tcp_nopush on;
- #HTTP連接的持續時間
- keepalive_timeout 30;
- #壓縮設置
- gzip on;
- gzip_types text/javascript text/plain text/css application/xml application/x-javascript;
- #Tomcat集羣配置
- upstream myserver {
- #通過client的IP進行映射
- ip_hash;
- # 負載均衡設置,weight越大越優先分配
- server localhost:18080 weight=5;
- server localhost:18081 weight=4;
- }
- server {
- listen 8080; #監聽的端口號
- server_name localhost;
- charset utf-8; #字符編碼方式
- #關閉日誌能夠減少IO
- #access_log logs/host.access.log main;
- location / {
- root html;
- index index.html index.htm;
- #代理配置
- proxy_pass http://myserver;
- #關閉重定向
- #proxy_redirect 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 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;
- #}
- # deny access to .htaccess files, if Apache's document root
- # concurs with nginx's one
- #
- #location ~ /\.ht {
- # deny all;
- #}
- }
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server {
- # listen 8000;
- # listen somename:8080;
- # server_name somename alias another.alias;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- # HTTPS server
- #
- #server {
- # listen 443;
- # server_name localhost;
- # ssl on;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- # ssl_session_timeout 5m;
- # ssl_protocols SSLv2 SSLv3 TLSv1;
- # ssl_ciphers HIGH:!aNULL:!MD5;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
一個機器上運行多個tomcat :
http://blog.csdn.net/T_ZZZ/article/details/76906599
redis緩存共享session :
1、下載三個jar ,下載地址:http://download.csdn.net/download/liu976180578/10134194
下好後,放入tomcat的lib裏面,每個tomcat 都要放。
2、打開每個tomcat 的 conf目錄下的context.xml文件,在最後一個節點</Context>上面添加。
<Manager className="com.radiadesign.catalina.session.RedisSessionManager" host="192.168.145.130" port="7000" database="0" maxInactiveInterval="60" />
Nginx詳情配置
安裝完成之後,配置目錄conf下有以下配置文件,過濾掉了xx.default配置:
linuxidc@ubuntu:/opt/nginx-1.7.7/conf$ tree |grep -v default . ├── fastcgi.conf ├── fastcgi_params ├── koi-utf ├── koi-win ├── mime.types ├── nginx.conf ├── scgi_params ├── uwsgi_params └── win-utf |
除了nginx.conf,其餘配置文件,一般只需要使用默認提供即可。
2.1.nginx.conf
nginx.conf是主配置文件,默認配置去掉註釋之後的內容如下圖所示:
l worker_process表示工作進程的數量,一般設置爲cpu的核數
l worker_connections表示每個工作進程的最大連接數
l server{}塊定義了虛擬主機
n listener監聽端口
n server_name監聽域名
n location{}是用來爲匹配的 URI 進行配置,URI 即語法中的“/uri/”。location / { }匹配任何查詢,因爲所有請求都以 / 開頭。
u root指定對應uri的資源查找路徑,這裏html爲相對路徑,完整路徑爲/opt/ opt/nginx-1.7.7/html/
u index指定首頁index文件的名稱,可以配置多個,以空格分開。如有多個,按配置順序查找。
從配置可以看出,nginx監聽了80端口、域名爲localhost、跟路徑爲html文件夾(我的安裝路徑爲/opt/nginx-1.7.7,所以/opt/nginx-1.7.7/html)、默認index文件爲index.html, index.htm、服務器錯誤重定向到50x.html頁面。
可以看到/opt/nginx-1.7.7/html/有以下文件:
linuxidc@ubuntu:/opt/nginx-1.7.7/html$ ls 50x.html index.html |
這也是上面在瀏覽器中輸入http://localhost,能夠顯示歡迎頁面的原因。實際上訪問的是/opt/nginx-1.7.7/html/index.html文件。
2.2.mime.types
文件擴展名與文件類型映射表,nginx根據映射關係,設置http請求響應頭的Content-Type值。當在映射表找不到時,使用nginx.conf中default-type指定的默認值。例如,默認配置中的指定的default-type爲application/octet-stream。
include mime.types;
default_type application/octet-stream;
默認
下面截一段mime.types定義的文件擴展名與文件類型映射關係,完整的請自行查看:
2.3.fastcgi_params
nginx配置Fastcgi解析時會調用fastcgi_params配置文件來傳遞服務器變量,這樣CGI中可以獲取到這些變量的值。默認傳遞以下變量:
這些變量的作用從其命名可以看出。
2.4.fastcgi.conf
對比下fastcgi.conf與fastcgi_params文件,可以看出只有以下差異:
linuxidc@ubuntu:/opt/nginx-1.7.7/conf$ diff fastcgi.conf fastcgi_params 2d1 < fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |
即fastcgi.conf只比fastcgi_params多了一行“fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;”
原本只有fastcgi_params文件,fastcgi.conf是nginx 0.8.30 (released: 15th of December 2009)才引入的。主要爲是解決以下問題(參考:
原本Nginx只有fastcgi_params,後來發現很多人在定義SCRIPT_FILENAME時使用了硬編碼的方式。例如,fastcgi_param SCRIPT_FILENAME /var/www/foo$fastcgi_script_name。於是爲了規範用法便引入了fastcgi.conf。
不過這樣的話就產生一個疑問:爲什麼一定要引入一個新的配置文件,而不是修改舊的配置文件?這是因爲fastcgi_param指令是數組型的,和普通指令相同的是:內層替換外層;和普通指令不同的是:當在同級多次使用的時候,是新增而不是替換。換句話說,如果在同級定義兩次SCRIPT_FILENAME,那麼它們都會被髮送到後端,這可能會導致一些潛在的問題,爲了避免此類情況,便引入了一個新的配置文件。
因此不再建議大家使用以下方式(搜了一下,網上大量的文章,並且nginx.conf的默認配置也是使用這種方式):
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
而使用最新的方式:
include fastcgi.conf;
2.5.uwsgi_params
與fastcgi_params一樣,傳遞哪些服務器變量,只有前綴不一樣,以uwsgi_param開始而非fastcgi_param。
2.6.scgi_params
與fastcgi_params一樣,傳遞哪些服務器變量,只有前綴不一樣,以uwsgi_param開始而非fastcgi_param。
2.7.koi-utf、koi-win、win-utf
這三個文件都是與編碼轉換映射文件,用於在輸出內容到客戶端時,將一種編碼轉換到另一種編碼。
koi-win: charset_map koi8-r < -- > windows-1251
koi-utf: charset_map koi8-r < -- > utf-8
win-utf: charset_map windows-1251 < -- > utf-8
koi8-r是斯拉夫文字8位元編碼,供俄語及保加利亞語使用。在Unicode未流行之前,KOI8-R 是最爲廣泛使用的俄語編碼,使用率甚至起ISO/IEC 8859-5還高。