Nginx基礎配置

聲明:自己學習記錄下來的一些配置,以後用到會慢慢完善。

一、介紹

Nginx:Web服務器/反向代理服務器及電子郵件代理服務器。輕量、支持高併發,坑多。

二、全局段配置

worker_prosesses 工作進程,最大爲CPU數*核心數

三、events

worker_connections 每個工作進程的連接數

四、http

autoindex on; 開啓顯示目錄
log_format simple ‘$remote_addr “$request” “$http_x_forwarded_for”’ 請求日誌的格式設置,命名爲simple。 分別爲請求ip,url,使用代理請求前的客戶ip
access_log /logs/nginx.log simple 所有請求以simple的格式寫入日誌文件

五、server

每一個server爲一個虛擬主機

    server{
        listen 80; #端口
        server_name www.a.com;#域名,還需更改本地host指向
        root    /var/www;#默認路徑
        location / {
            #root /var/www;
            index index.php index.html;
            try_files	$uri	/index.php?$uri;#重定向,省略index.php
        }
        #添加php解析
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

六、反向代理

反向代理服務器IP 192.168.6,真實服務器192.168.1.7

#設置圖片代理
location ~ \.(jpg|jpeg|png|gif)$ {
	#真實服務器
	proxy_pass  http://192.168.1.7;        
	#此參數把用戶ip帶給真實服務器,和日誌內http-x-forwarded-for相關。 
	proxy_set_header    X-Forwarded-For $remote_addr;  
}

七、負載均衡

兩臺負載均衡資源服務器192.168.1.7, 192.168.1.8。

#在http內
upstream imgServer{
	server 192.168.1.7:80 weight=2 max_fails=2 fail_timeout=30s;
	server 192.168.1.8:80 weight=1 max_fails=2 fail_timeout=30s;
}
反向代理 proxy_pass http://192.168.1.7改爲proxy_pass http://imgServer;
weight 爲權重
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章