若 make 的時候出現下面錯誤
make -f objs/Makefile
make[1]: Entering directory `/home/user/src/server/nginx-1.0.14'
cd /usr/lib \
&& if [ -f Makefile ]; then make distclean; fi \
&& CC="gcc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \
./configure --disable-shared
/bin/sh: line 2: ./configure: No such file or directory
make[1]: *** [/usr/lib/Makefile] Error 127
make[1]: Leaving directory `/home/user/src/server/nginx-1.0.14'
make: *** [build] Error 2
解決方式:在 configure 時 --with-pcre 指向 pcre 的 source tree
$ ./configure --with-pcre=~/pcre-8.30 $ make
基本指令
# 啟動 /usr/local/nginx/sbin/nginx # 停止 /usr/local/nginx/sbin/nginx -s stop # Reload /usr/local/nginx/sbin/nginx -s reload # 編輯設定檔 vim /usr/local/nginx/conf/nginx.conf # 檢查設定檔 /usr/local/nginx/sbin/nginx -t
下面列出幾種常用的模式:
Forward proxy
又稱正向代理
server {
listen 8080;
resolver 168.95.1.1;
access_log off;
location / {
proxy_pass http://$host$request_uri;
proxy_set_header Host $http_host;
proxy_buffers 256 4k;
}
}
再來可以設定瀏覽器proxy為localhost:8080,然後開網頁看看
或是用wget測試:
http_proxy=http://localhost:8080/ use_proxy=on wget -S -O - http://google.com/
Reverse proxy (Loading balance)
又稱反向代理,藉由nginx當gateway再導到後端sever,達到load balance和保護的效果upstream backend {
server 10.0.0.1:80;
server 10.0.0.2:80;
}
server {
listen 8080;
location / {
proxy_pass http://backend/;
}
}
可以參考有关nginx upstream的五种分配方式,分別是:round-robin、weight、ip_hash、fair、url_hash
Virtual host
又稱虛擬主機,多個domain name指到同一個IP時,nginx可以依據domain name使用不同的server(or directory)來處理為了方便管理,我們可以把一個virtual host寫成一個設定檔,然後在nginx.conf去include
http {
include /usr/local/nginx/conf/vhosts/*;
}
conf/vhosts/elephant.com.conf
server
{
server_name elephant.com;
listen 8000;
location / {
index index.html;
root /var/www/elephant.com/htdocs;
}
}
conf/vhosts/lion.com.conf
server
{
server_name lion.com;
listen 8000;
location / {
index index.html;
root /var/www/lion.com/htdocs;
}
}
重啓nginx後,使用 http://elephant.com:8000 與 http://lian.com:8000 來測試看看吧
(可以先在 /etc/hosts 把上面兩個hostname指到localhost)
Enable SSL
configure 時加入 --with-http_ssl_module設定檔如下
server
{
server_name localhost;
listen 443;
ssl on;
ssl_certificate /usr/local/nginx/conf/server.crt;
ssl_certificate_key /usr/local/nginx/conf/server.key;
location / {
index index.html;
root /var/www/html;
}
}
參考資料
HttpCoreModule - Directives 與 Variables 解說
蚊子館: Nginx Web伺服器 - 這個網站列了更多nginx的使用方式

沒有留言:
張貼留言