首先先看一下nginx/conf/nginx.conf 配置文件内的信息:php
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#下面这个server就是默认的主机信息
#listen 端口, server_name 主机名称, location-root 网站根目录,location-index 网站默认页面
server {
listen 80;
server_name localhost;
#charset koi9-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#咱们之后本身配置虚拟主机的话 就在这个地方新添加server便可
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
因此咱们部署静态网站,把静态的html页面相关放到html这个目录下便可,固然也能够本身定义目录,修改配置文件便可html
那么若是咱们想在一个nginx上部署多个网站(不一样端口),应该怎么作呢?nginx
虚拟主机,也叫“网站空间”,就是把一台运行在互联网上的物理服务器划分红多个“虚拟”服务器。虚拟主机技术极大的促进了网络技术的应用和普及。同时虚拟主机的租用服务也成了网络时代的一种新型经济形式。web
1,将网站web1(web1网站的全部静态文件,好比web001.html),web2,分别上传到/usr/local/nginx/web1和/usr/local/nginx/web2下浏览器
2,修改nginx/conf/nginx.conf 配置文件:(添加两个server信息)服务器
server {
listen 81;
server_name localhost;
location / {
root web1;
index web001.html;
}
}
server {
listen 82;
server_name localhost;
location / {
root web2;
index web002.html;
}
}
3,重启nginx网络
./nginx -s reload
4,用浏览器访问ip+portsession
咱们上面部署的静态网站目前只能经过ip+port访问,那么若是想经过域名访问呢?app
答案很简单,只须要修改咱们刚才添加到配置文件中的server信息中的server_name:tcp
server {
listen 80;
server_name web1.zy.com;
location / {
root web1;
index web001.html;
}
}
server {
listen 80;
server_name web2.zy.com;
location / {
root web2;
index web002.html;
}
}
前提是你须要有这个域名,并将这个域名指向了这个ip。