Nginx實現多個站點使用一個端口(配置Nginx的虛擬主機)


Nginx 是一個輕量級高性能的 Web 服務器, 並發處理能力強, 消耗資源小, 無論是靜態服務器還是網站, Nginx 表現更加出色, 作為 Apache 的補充和替代使用率越來越高,目前很多大型網站都在使用Nginx做為 Web 服務器,例如:人人網。另外淘寶研發大軍針對大訪問量網站的需求,對Nginx做了專門的定制,添加了很多高級功能和特性(Tengine),Tengine的性能和穩定性已經在大型的網站如淘寶網天貓商城等得到了很好的檢驗。

本文將講解如何在Ubuntu Linux上使用 Nginx Web服務器來實現一台電腦一個端口(80)搭建多個網站。

絕大多數的 Nginx 運行在 Linux 機器上, 雖然有 Windows 移植版,但在Windows下的測試發現Nginx發揮不是很好. 所以本文將以 Linux 為例講解, 而 Mac OS 或其他 Unix like 機器上的操作應該是一樣的.

Nginx版本

lg@lg-PC:~$ nginx -v
nginx version: nginx/1.3.10

nginx配置文件默認目錄結構

/etc/nginx/
├── conf.d
│   ├── default.conf
│   ├── example_ssl.conf
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf

1 directory, 11 files

增加 Nginx 虛擬主機

配置 Virtual host 步驟如下:

1.檢查/etc/nginx/nginx.conf配置文件,確保文件中有:include /etc/nginx/conf.d/*.conf;   例如:

user  lg;
worker_processes  2;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
    debug_connection 127.0.0.1;
    debug_connection 192.168.1.0/24;
}
http {
    include       /etc/nginx/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"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    client_max_body_size        13m;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

2.關鍵步驟,在目錄/etc/nginx/conf.d/下面新建文件site1.conf,site2.conf,文件名任意寫,自己看明白就OK,后綴名需要與步驟1配置的一致,這里為.conf。site1代表我們的第一個站點,site2代表我們的第二個站點,下面我們看看兩個文件都需要寫點什么:

site1.conf:

server {
	listen		80;
	server_name	~^\d+\.\d+\.\d+\.\d+$;
	#charset koi8-r;
	error_page  404  /404.html;
	# redirect server error pages to the static page /50x.html
	#
	error_page   500 503 504  /50x.html;
	error_log	/var/log/nginx/debug.log debug;
	index	index.html index.htm;
	root /home/lg/www/;

	location /svn {
		root /home/lg/www/;
		index index.html;
	}

        location = /favicon.ico {
        	try_files $uri $uri/favicon.ico /home/lg/www/favicon.ico =404;
        }

	location /share {
		root /home/lg/Downloads;
	}

        # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store(Mac).
        location ~ /\. {
            deny all;
        }

	location ^~ /packages {
		root /home/lg/Downloads/1software;
		autoindex on;
		autoindex_exact_size on;
		autoindex_localtime on;
		allow	all;
	}

	location ^~ /Music {
		root /home/lg/;
		autoindex on;
		autoindex_exact_size on;
		autoindex_localtime on;
		allow	all;
	}

	location ^~ /Videos {
		root /home/lg/;
		autoindex on;
		autoindex_exact_size on;
		autoindex_localtime on;
		allow	all;
	}

	location ^~ /html5 {
		root /home/lg/workspace/nodejs/;
		index index.html index.htm;
	}

	location ^~ /NginxStatus {
		stub_status	on;
		access_log	on;
		#auth_basic	'NginxStatus';
		#auth_basic_user_file	conf.d/htpasswd
	}

	location = /50x.html {
		root   /usr/share/nginx/html;
	}

	location = /404.html {
		root   /usr/share/nginx/html;
	}
}

 

site2.conf:

server {
    listen       80;
    server_name  ~^openlg.net$;
    root   /home/lg/workspace/phpworkspace/wp;
    index index.php index.html index.htm;
    location = /favicon.ico {
        try_files /home/lg/www/favicon.ico =404;
        #log_not_found off;
	#access_log off;
    }

    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store(Mac).
    location ~ /\. {
	deny all;
    }

    location ~* /(?:uploads|files)/.*\.php$ {
	deny all;
    }

    location / {
	try_files $uri $uri/ /index.php?$args;
    }

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log off;
        log_not_found off;
        expires max;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
	expires 24h;
	log_not_found off;
    }

    error_page  404              /404.html;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
	try_files $uri =404;
	fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
	fastcgi_pass   127.0.0.1:9000;
    }
}

3.測試配置文件,沒問題就加載新配置文件

lg@lg-PC:~$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
lg@lg-PC:~$ sudo kill -HUP `cat /var/run/nginx.pid`
lg@lg-PC:~$

4.打開文件/etc/hosts,添加

127.0.0.1		openlg.net

5.打開瀏覽器分別請求下面的地址進行測試,如果相應內容不一樣,那么咱們就大功告成了。

http://127.0.0.1

http://openlg.net

到這里,大家也許已經明白怎么回事了,我再羅嗦兩句,我這里的site1.conf相當與一個靜態文件服務器,site2.conf是一個php的網站,大家可以看到配置文件中加粗顯示的兩行:listen和server_name分別代表監聽端口和虛擬主機名稱。80端口沒得說,重點解釋下server_name,server_name用的是正則表達式,~^\d+\.\d+\.\d+\.\d+$匹配所有的ip地址,~^openlg.net$匹配openlg.net,這里也可以直接寫成server_name    127.0.0.1;或者server_name      openlg.net;

當我們請求http://127.0.0.1/時,nginx會使用兩個server_name配置的正則表達式分別去測試請求地址中的127.0.0.1來決定使用那個虛擬主機,這里當然就是使用site1.conf中配置的server了。當我們請求http://openlg.net/時,nginx就會選擇使用site2.conf中配置的server了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM