nginx基本域名 端口 IP 的虛擬主機

基於域名

##nginx併發連接數   processes 乘  connection
## worker_processes   服務進程數,一般和cpu 核心一致即可
worker_processes  1;     

## 可加在main http server location 裏,可設置分別的模塊日誌
##生產環境一般用  warn error crit,默認error
error_log logs/error.log [debug|warn|error|crit];

events {
 ##一個worker同時服務數量
    worker_connections  1024;
}
http {
    include       mime.types;
            ## include    extra/www.conf; 包括extra下的配置文件
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
        ##定義訪問日誌記錄格式 以及訪問日誌保存路徑,可放於http
        ##location 模塊裏
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  logs/access.log  main;
  ### 加下面這個server 塊 禁止 IP訪問  
        server {
       listen 80 default;
       server_name _;
       return 403;
}
        ##加下面模塊,可通過status.etiantian.org 查看網站訪問次數
        ##active connection  活動連接,即併發數
        ## server accepts handled requests 三個值分別 
        ##1.一共處理的鏈接  2.成功創建多少次握手 3.已經處理完畢
        ## reading:讀取客戶端header信息數量 writing:返回客戶端數量
        ## waiting :已經處理完等待下一次請求的滯留鏈接
server{
server_name status.etiantian.org;
##也可在已有的虛擬主機加下面這個location
 location / {
 stub_status on;
access_log off;
  }
}

        server {
        listen       80;
                ##server_name 後空格加多個域名即起到別名的作用,別名
                ##也需要做DNS解析 別名的優勢既可以對外方便訪問
                ##也可以對內部集羣做監控用,因爲集羣對外都是同一個域名 
                ##設置別名可以分別監測每一臺服務器
        server_name  www.etiantian.org etiantian.org ;
        location / {
            root   html/www;   
            index  index.html index.htm;
        }
   }
    server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
      }
     server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
     }
}
##例子 www.arrow.org/document 跳轉 www.baidu.com
### 其它則 顯示html/www/index.html
 server {
        listen       80;
        server_name  www.arrow.org;
       # autoindex on;
        rewrite ^(.*)/document/ http://www.baidu.com break;
        location / {
                #return 404;
            root   html/www;
            index  index.html;
        }

   }

基於端口

listen 80;
listen 81;
listen 82;
###對應server模塊部分直接修改即可,這時外部訪問需要指定
域名+端口的訪問形式,最終解析成 IP:端口 的形式,這時最終會映射到對應端口的server部分,無視域名

基於IP訪問

首先需要有多個IP

listen 10.0.0.100:80;
listen 10.0.0.101:80;
listen 10.0.0.102:80;

對應server部分對應這個就可以

配置文件分離保存,並不是所有的配置都可以

在/usr/local/nginx/conf 下創建一個目錄,將配置文件分離保存在這個目錄裏,便於模塊化管理

mkdir -p /usr/local/nginx/conf/extra

nginx.conf將server模塊分出來,替代成

include extra/*.conf;
include extra/www.conf;

nginx 日誌切割

## 創建文件一個文件,將日誌mv過去,直接重啓,新的access_log
##會自動生成
vim cut_nginx_log.sh
cd /usr/local/nginx/logs
/bin/mv access_logs /data/logs/access_logs$(date -I).log
/usr/local/nginx/sbin/nginx -s reload

############################################

nginx location

location(相當於if else) 要用用戶請求URI(URL) 來執行不同的應用
語法
location [=|~|~|@|^~] url { } 匹配
~ !~取反.用於區分大小寫匹配
~
!~ 取反.不區分大小寫匹配,也可用於邏輯操作符
= 精確匹配
^~ 優先匹配
匹配優先級 = ^~ ~
.(gif|jpg正則匹配) /document/ /
###########################################

nginx rewrite

rewrite 實現url地址改寫重寫. 需要PCRE支持
apache nginx resin tomcat 都支持,複雜的需要開發去實現
最簡單的可以運維搞定

rewrite語法 rewrite regex replacement[flag];
位置 server location if
例子:
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
rewrite /document/ http://www.baidu.com permanent;
匹配所有

flag: permanent 301 永久重定向,顯示跳轉後的地址
break 匹配完即終止,不再匹配後面的任何規則
redirect 302 臨時重定向,顯示跳轉後的地址
last 本條匹配完後 繼續向下匹配新的URL規則

rewrite 在企業中100%會用到
規範URL名字更美觀,動態URL更容易被收錄
############################################

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章