1. 程式人生 > >nginx(虛擬主機配置、ip、域名、埠號)

nginx(虛擬主機配置、ip、域名、埠號)

本章主要講解nginx的虛擬主機。分別是基於域名。基於ip、基於埠號來配置虛擬主機。

大致配置細節在nging.conf的http{}內編寫:一下列出簡單的配置,詳情請看同類文章的nginx(nginx.conf詳解)的說明:這裡不過多介紹。主要配置細節:

// 全域性區
worker_processes 1; // 有1個工作的子程序,可以自行修改,但太大無益,因為要爭奪CPU,一般設定為 CPU數*核數
Event {
// 一般是配置nginx連線的特性
// 如1個word能同時允許多少連線
 worker_connections  1024; // 這是指 一個子程序最大允許連1024個連線
}
http {  //這是配置http伺服器的主要段
     Server1 { // 這是虛擬主機段       
            Location {  //定位,把特殊的路徑或檔案再次定位 ,如image目錄單獨處理
            }             /// 如.php單獨處理
     }
     Server2 {
     }
}

server{}:配置虛擬主機必須有這個段。

server_name:虛擬主機的域名,可以寫多個域名,類似於別名,比如說你可以配置成 server_name b.ttlsa.com c.ttlsa.com d.ttlsa.com,這樣的話,訪問任何一個域名,內容都是一樣的 listen 80,監聽ip和埠,這邊僅僅只有埠,表示當前伺服器所有ip的80埠,如果只想監聽127.0.0.1的80,寫法如下: listen 127.0.0.1:80。

root /data/site/b.ttlsa.com:站點根目錄,你網站檔案存放的地方。注:站點目錄和域名儘量一樣,養成一個好習慣。

access_log/data/logs/nginx/b.ttlsa.com-access.log main:訪問日誌 location /{} 預設uri。

基於域名的配置

配置伺服器:修改nginx.config的http的server

1、編輯nginx的核心配置檔案nginx.conf

vim /usr/local/nginx/sbin/nginx.config

2、新增一個server

server {
	listen	80;
	server_name	z.com;
	location / {
	     root z.com;
	     index index.html;
	}
 }

3、在相對目錄下建立檔案和資料夾

Mkdir  z.com(注意是在/usr/local/nginx/下面建立)
Mkdir	z.com/index.html(index.html裡面可以隨便一些訪問的內容)

4、 然後配置繫結ip到域名上:/etc/hosts

127.0.0.1   localhost
192.168.0.2	z.com#配置域名到ip其他地方不用動(前面的192.168.0.2是本機靜態ip)
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

5、然後啟動nginx訪問網頁。

/usr/local/nginx/sbin/nginx -s relaod(軟重啟)

基於埠

1、  編輯nginx的核心配置檔案nginx.conf

/usr/local/nginx/sbin/nginx -s relaod(軟重啟)


2、新增一個server

server {
	listen	5555;#修改監聽的埠是5555
	server_name	z.com;
	location / {
	     root z.com;#此處可以使用相對路徑也可以使用絕對路徑,相對路徑是相對於/usr/local/nginx/下面為相對路徑,而絕對路徑是從/usr/…..為絕對路徑
	     index index.html;
	}
 }

3、在相對目錄下建立檔案和資料夾

Mkdir  z.com(注意是在/usr/local/nginx/下面建立)
Mkdir	z.com/index.html(index.html裡面可以隨便一些訪問的內容)


4、然後配置繫結ip到域名上:/etc/hosts

127.0.0.1   localhost
192.168.0.2	z.com#配置域名到ip其他地方不用動
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

5、然後啟動nginx訪問網頁。

/usr/local/nginx/sbin/nginx -s relaod(軟重啟)

基於ip

1、編輯nginx的核心配置檔案nginx.conf

vim /usr/local/nginx/sbin/nginx.config

2、新增一個server

server {
	listen	80;
	server_name	192.168.0.2;#需要使用ifconfig檢視本機的ip是多少
	location / {
	     root z.com;#此處可以使用相對路徑也可以使用絕對路徑,相對路徑是相對於/usr/local/nginx/下面為相對路徑,而絕對路徑是從/usr/…..為絕對路徑
	     index indexIp.html;
	}
 }

3、在相對目錄下建立檔案和資料夾

Mkdir  z.com(注意是在/usr/local/nginx/下面建立)
Mkdir	z.com/index.html(index.html裡面可以隨便一些訪問的內容)

4、然後配置繫結ip到域名上:/etc/hosts

127.0.0.1   localhost
192.168.0.2	z.com#配置域名到ip其他地方不用動
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

5、然後啟動nginx訪問網頁。

/usr/local/nginx/sbin/nginx -s relaod(軟重啟)