接下來咱們就開始學習nginx的設定檔吧
然後cd到etc/nginx目錄下
第一部分 nginx.conf
主設定檔為nginx.conf
與php相關的是fastcgi_params
與python相關的是uwsgi_params
nginx的注意事項1:恩,首先我們先確保先把apache停了,因為Apache預設使用80連接埠,兩個會搶的!
我們首先可以開啟nginx.conf
#使用的使用者和組user www-data;#指定工作繁衍的處理序數(預設為CPU的線程數,咱的是N2600,雙核四線程!)worker_processes 4;#指定pid存放的路徑,應該記錄了nginx守護進程的進程號,是其他進程的父進程id!(如Apache下有PidFile )pid /var/run/nginx.pid;events {use epoll;#linux下效能最好的event模式!預設木有這條!#允許的串連數worker_connections 768;# multi_accept on;}http {### Basic Settings##sendfile on;tcp_nopush on;tcp_nodelay on;#keepalive_timeout 測試多次請求傳輸之間的時間,再一次持久連結,如果用戶端的兩次HTTP請求超過keepalive_timeout的時間,則關閉連結釋放資源!!(Apache下也有)keepalive_timeout 65;types_hash_max_size 2048;# server_tokens off;# server_names_hash_bucket_size 64;# server_name_in_redirect off;include /etc/nginx/mime.types;#啥是mime?我們的電腦網路裡貌似提到了,但咱得搞懂!#default_type application/octet-stream;#當使用者請求的檔案木有在伺服器中定義mime類型映射,使用DefaultType#可以有啥? text/html text/plain(表示尾碼為txt的檔案以文本顯示) image/gif#這裡的application/octet-stream 是啥?#若未定的檔案多是軟體或者映像,建議為application/octet-stream#瀏覽器會提示使用者以“另存新檔”的方式儲存檔案!### Logging Settings###日誌,詳細見後面access_log /var/log/nginx/access.log;error_log /var/log/nginx/error.log;### Gzip Settings##開啟gzip壓縮gzip on;gzip_disable "msie6";# gzip_vary on;# gzip_proxied any;# gzip_comp_level 6;# gzip_buffers 16 8k;# gzip_http_version 1.1;# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;### Virtual Host Configs##include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;##很好,我們可以清楚的看到,我們會載入conf.d目錄與sites-enabled目錄下得所有檔案,這也證明了我們為啥子可以在這兩個目錄下寫設定檔!}#nginx同樣可以做郵件Proxy 伺服器!#mail {# # See sample authentication script at:# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript## # auth_http localhost/auth.php;# # pop3_capabilities "TOP" "USER";# # imap_capabilities "IMAP4rev1" "UIDPLUS";## server {# listen localhost:110;# protocol pop3;# proxy on;# }## server {# listen localhost:143;# protocol imap;# proxy on;# }#}
第二部分 nginx的虛擬機器主機配置
啥是虛擬機器主機?話說我剛進精弘的任務就是配置虛擬機器主機,當時覺得好神奇啊!哈哈!首先虛擬機器主機是把一台主機分成一台台“虛擬”的主機,你想想我有20幾個網站,咱不是大款,木有20幾台伺服器,於是利用虛擬機器主機,就可以解決這個問題!
如是最簡單的虛擬機器主機設定檔
與Apache相同,nginx也可以配置多種類型的虛擬機器主機。
- 基於網域名稱的虛擬機器主機
- 基於IP的虛擬機器主機
- 基於連接埠的虛擬機器主機
然後我們發現目錄下有site-available與site-enabled兩個目錄,和Apache一模一樣,我們一般採用的方法是在前者下寫好設定檔,到後者目錄下做好一個軟串連!原因如同目錄的名字一樣,前者是存在的網站,而後者是正在使用的目錄!nginx預設會載入site-enabled目錄!前者的目錄下有一個default給我們參考如何寫虛擬機器主機的設定檔
讓我們來看一下這段:
# You may add here your# server {# ...# }# statements for each of your virtual hosts to this file#這段文字很清晰告訴我們如何添加虛擬機器主機!### You should look at the following URL's in order to grasp a solid understanding# of Nginx configuration files in order to fully unleash the power of Nginx.# http://wiki.nginx.org/Pitfalls# http://wiki.nginx.org/QuickStart# http://wiki.nginx.org/Configuration## Generally, you will want to move this file somewhere, and start with a clean# file but keep this around for reference. Or just disable in sites-enabled.## Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.####上面這段講了具體可以參考那些網站和例子!server {#開始了直接server打頭,如果配置在nginx.conf裡,請寫在http{……server{……}}裡,咱們是基於http協議的!#listen 80; ## listen for ipv4; this line is default and implied#監聽80連接埠!#listen [::]:80 default ipv6only=on; ## listen for ipv6#root HTML網頁檔案存放的目錄root /usr/share/nginx/www;#預設首頁檔案,順序從左往右!如果找不到index.html,就去找index.htm`index index.html index.htm;# Make site accessible from http://localhost/server_name localhost;#主機名稱location / {# First attempt to serve request as file, then# as directory, then fall back to index.htmltry_files $uri $uri/ /index.html;}#顯而易見,訪問根目錄,直接跳轉到首頁!location /doc {root /usr/share;autoindex on;allow 127.0.0.1;deny all;}location /images {root /usr/share;autoindex off;}#這裡涉及了404,50x的頁面及其地址#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/www;#}# 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$ {# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# 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;# root html;# index index.html index.htm;## location / {# try_files $uri $uri/ /index.html;# }#}# HTTPS server##server {# listen 443;# server_name localhost;## root html;# index index.html index.htm;## ssl on;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;## ssl_session_timeout 5m;## ssl_protocols SSLv3 TLSv1;# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;# ssl_prefer_server_ciphers on;## location / {# try_files $uri $uri/ /index.html;# }#}
我們可以發現要配多個虛擬機器主機基本的格式是
server{……}
server{……}
然後我們來看一下nginx的日誌
第三部分 nginx的日誌與nginx日誌相關的指令主要有兩條
log_format #設定日誌的格式
access_log #指定記錄檔的存放路徑,格式,緩衝大小
兩條指令即可放在http{……}或者server{……}之間
log_format XXXX(日誌的名字,不能重複!)'$remote_addr [$time_local] ''"$request" $status $body_bytes_sent ''"$http_referer" "$http_user_agent" ''$http_x_forwarded_for';$remote_addr #反向 Proxy伺服器IP地址$time_local #用於記錄訪問時間與時區$request #用於記錄請求的URL與HTTP協議$status #用於記錄請求狀態$body_bytes_sent #發送給用戶端的檔案主題內容大小$http_referer #記錄從那個頁面連結訪問過來的$http_user_agent #用於記錄用戶端瀏覽器的相關資訊$http_x_forwarded_for #記錄用戶端IP地址和用戶端請求的伺服器位址(這是由於是反向 Proxy伺服器,web伺服器無法的IP)直接拿到用戶端
access_log
access_log path [format {buffer=size | off}]
#不使用日誌
access_log off
#使用預設XXXX格式的日誌
access_log /data/logs/file.log XXXX
然後來一個實際的例子,這是我們精弘網路的測試伺服器上的nginx下的一段虛擬機器主機設定檔!
#ServerName bbs.zjut.com#ServerAlias v.zjut.com bbs2.zjut.com bbs.zjut.edu.cn#DocumentRoot /opt/www/bbs#jump to bbs.zjut.com#server{ listen 80;# server_name bbs2.zjut.com v.zjut.com ;# rewrite ^/(.*)$ http://bbs.zjut.com/$1 permanent;# access_log off;#}#jump to bbs.zjut.inserver{ listen 80;#監聽80連接埠server_name bbs2.zjut.in ;#主機名稱rewrite ^/(.*)$ http://bbs.zjut.in/$1 permanent;access_log off;#日誌關閉}#bbs.zjut.comserver{ listen 80;#監聽80連接埠server_name bbs.zjut.in;root /opt/www/bbs;#目錄放在/opt/www/bbsindex index.html index.htm index.php;#先找index.html,然後index.htm,再是index.phplog_format bbs_log '$remote_addr [$time_local] ''"$request" $status $body_bytes_sent ''"$http_referer" "$http_user_agent" ''$http_x_forwarded_for';#定義日誌格式# access_log /opt/log/nginx/bbs/bbs.log bbs_log;error_page 404 = /errorpage/404.html;#404錯誤頁面的地址error_page 500 502 503 504 = /errorpage/500.html;#50X錯誤地址的目錄location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)${ expires 30d; }location ~ .*\.(js|css|asp|aspx)?${ expires 1h; }location /UploadFile {return 404;}location /uploadfile {return 404;}# location ~ .*\.(sh|bash|asp)?$# { return 403; }#location ~* \.(html|htm|xml|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|flv|txt|exe|zip|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$#{ access_log off;# include /etc/nginx/conf.d/ipdeny.conf;#}location ~ .*\.(php|php5)?$ {#反向 Proxy到原生8000連接埠,我們可以去Apache的site-enabled目錄下發現該站在Apache監聽的就是8000連接埠!proxy_pass http://127.0.0.1:8000;# access_log off;# include /etc/nginx/conf.d/ipdeny.conf;}# error_page 403 http://whatwhat.xxx/sss.html}
關於nginx的日誌,還是很不瞭解,希望在接下來的系列學習文章有所體現。