nginx配置虛擬主機

nginx 是一個小巧高效的 web 服務器,由俄羅斯程序員 Igor Sysoev 開發,nginx 雖然體積小,但功能一點也不弱,能和其餘的 web 服務器同樣支持 virtual hosting,即一個IP對應多個域名以支持多站點訪問,就像一個IP對應一個站點同樣,因此是」虛擬」的。php

這裏以配置2個站點(2個域名)爲例,n 個站點能夠相應增長調整,假設:html

IP地址: 202.55.1.100
域名1 ysy1.com 放在 /www/ysy1
域名2 ysy22.com 放在 /www/ysy2nginx

配置 nginx virtual hosting 的基本思路和步驟以下:程序員

把2個站點 ysy1.com, ysy2.com 放到 nginx 能夠訪問的目錄 /www/
給每一個站點分別建立一個 nginx 配置文件 ysy1.com.conf,ysy2.com.conf, 並把配置文件放到 /etc/nginx/vhosts/
而後在 /etc/nginx.conf 裏面加一句 include 把步驟2建立的配置文件所有包含進來(用 * 號)
重啓 nginxweb

 

具體步驟:vim

一、在 /usr/local/etc/nginx 下建立 vhosts 目錄服務器

1 mkdir /etc/nginx/vhosts

 

 

二、在 /usr/local/etc/nginx/vhosts/ 裏建立一個名字爲 ysy1.com.conf 的文件,把如下內容拷進去session

 1 server {
 2         listen  80;#端口要和nginx端口同樣,個人由8080默認端口改到80
 3         server_name ysy1.com www.ysy1.com;
 4 
 5         #access_log  /www/access_ysy1.log;
 6 
 7         location / {
 8             root   /www/ysy1.com;#放置訪問文件的目錄,
 9             index  index.php index.html index.htm;#訪問文件的檢索順序
10         }
11 
12         # error_page   500 502 503 504  /50x.html;
13         location = /50x.html {
14             root   html;  #隨便,這裏我沒有設置
15         }
16 
17        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
18         location ~ \.php$ {
19             fastcgi_pass   127.0.0.1:9000;
20             fastcgi_index  index.php;
21             fastcgi_param  SCRIPT_FILENAME  /www/ysy1.com/$fastcgi_script_name;#fastcgi須要配置
22             include        fastcgi_params;
23         }
24         location ~ /\.ht {
25             deny  all;
26         }
27 }

 

 

 

三、在 /usr/local/etc/nginx/vhosts/ 裏建立一個名字爲 ysy2.com.conf 的文件,把上邊內容拷進去,將全部ysy1改稱ysy2app

 

四、打開 /usr/local/etc/nginix.conf 文件,在相應位置加入 include 把以上2個文件包含進來tcp

  1 user  www www;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 
 34     server {
 35         listen       80;
 36         server_name  localhost;
 37 
 38         #charset koi8-r;
 39 
 40         #access_log  logs/host.access.log  main;
 41 
 42         location / {
 43             root   html;
 44             index  index.html index.htm;
 45         }
 46 
 47         #error_page  404              /404.html;
 48 
 49         # redirect server error pages to the static page /50x.html
 50         #
 51         error_page   500 502 503 504  /50x.html;
 52         location = /50x.html {
 53             root   html;
 54         }
 55 
 56         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 57         #
 58         #location ~ \.php$ {
 59         #    proxy_pass   http://127.0.0.1;
 60         #}
 61 
 62         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 63         #
 64         location ~ \.php$ {
 65             root           html;
 66             fastcgi_pass   127.0.0.1:9000;
 67             fastcgi_index  index.php;
 68             fastcgi_param  SCRIPT_FILENAME  /usr/local/Cellar/nginx/1.6.2/html$fastcgi_script_name;
 69             include        /usr/local/etc/nginx/fastcgi_params;
 70         }
 71 
 72         # deny access to .htaccess files, if Apache's document root
 73         # concurs with nginx's one
 74         #
 75         #location ~ /\.ht {
 76         #    deny  all;
 77         #}
 78     }
 79 
 80 
 81     # another virtual host using mix of IP-, name-, and port-based configuration
 82     #
 83     #server {
 84     #    listen       8000;
 85     #    listen       somename:8080;
 86     #    server_name  somename  alias  another.alias;
 87 
 88     #    location / {
 89     #        root   html;
 90     #        index  index.html index.htm;
 91     #    }
 92     #}
 93 
 94 
 95     # HTTPS server
 96     #
 97     #server {
 98     #    listen       443 ssl;
 99     #    server_name  localhost;
100 
101     #    ssl_certificate      cert.pem;
102     #    ssl_certificate_key  cert.key;
103 
104     #    ssl_session_cache    shared:SSL:1m;
105     #    ssl_session_timeout  5m;
106 
107     #    ssl_ciphers  HIGH:!aNULL:!MD5;
108     #    ssl_prefer_server_ciphers  on;
109 
110     #    location / {
111     #        root   html;
112     #        index  index.html index.htm;
113     #    }
114     #}
115 
116 
117     #include all vhosts
118     include /usr/local/etc/nginx/vhosts/*.conf;
119 }

 

 

五、創建nginx對應的訪問目錄及文件

1 sudo mkdir /www/ysy1.com/
2 vim /www/ysy1.com/index.php

 

index.php內部調用phpinfo(); 

6,修改hosts配置,重啓 Nginx

1 sudo vim /etc/hosts
 1 cat /etc/hosts
 2 ##
 3 # Host Database
 4 #
 5 # localhost is used to configure the loopback interface
 6 # when the system is booting.  Do not change this entry.
 7 ##
 8 127.0.0.1    localhost
 9 255.255.255.255    broadcasthost
10 ::1             localhost 
11 fe80::1%lo0    localhost
12 127.0.0.1       ysy1.com
13 127.0.0.1       ysy2.com
14 127.0.0.1       www.ysy1.com #訪問什麼配什麼,與conf配置文件無關
15 127.0.0.1       www.ysy2.com
相關文章
相關標籤/搜索