Nginx(二)– 配置文件之虛擬主機配置
- 2019 年 12 月 9 日
- 筆記
1.配置文件與解釋
#user nobody;worker_processes 1; # 設置工作子進程,默認是1個工作子進程,可以修改,一般設置為CPU的總核數 #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 80; server_name localhost; #charset koi8-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; #} } # 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; # } #}}
2.Nginx虛擬主機配置
1.基於域名配置(使用的比較多)
1) 在nginx/conf/nginx.conf文件中的http段中添加一個server,如下所示:
1 # 基於域名的虛擬主機配置2 server {3 listen 80;4 server_name www.xbq.com;5 location / {6 root html/host; # 相對路徑,在 nginx/html/host目錄中7 index admin.html;8 }9 }
2) 在nginx/html文件夾中新建 host 文件夾,然後在 host文件夾中 新建admin.html文件,admin.html文件中的內容為:
Hello,This is host page,www.xbq.com.
3) 重新載入nginx.conf文件,./nginx -s reload
4) 修改C:WindowsSystem32driversetchosts文件,添加如下內容,為了將域名解析:

5) 瀏覽器訪問:www.xbq.com,發現和剛剛寫的admin.html內容一樣,成功!

當訪問www.xbq.com的時候,會匹配server中 server_name,然後找到html/host文件夾中的admin.html,返回介面。
2.基於埠配置
1) 在nginx/conf/nginx.conf文件中的http段中添加一個server,如下所示:
1 # 基於埠號的虛擬主機配置2 server {3 listen 8888;4 server_name test; # 無實際意義,可省略5 location / {6 root html/port; # 相對路徑,在nginx/html/port目錄中7 index admin.html;8 }9 }
2) 在nginx/html文件夾中新建 port文件夾,然後在 port文件夾中 新建admin.html文件,admin.html文件中的內容為:
Hello,This is port page.
3) 重新載入nginx.conf文件,./nginx -s reload
4) 瀏覽器訪問:http://192.168.80.128:8888/,出現如下,則成功:

3.基於IP配置(使用的比較少)
(1) 先查看本機的IP,ifconfig

(2) 添加虛擬網卡
ifconfig eth0:1 192.168.80.150 broadcast 192.168.80.255 netmask 255.255.255.0 up
route add -host 192.168.80.150 dev eth0:1
(3) 檢測網路是否通:ping 192.169.80.150

(4) 在nginx/conf/nginx.conf文件中的http段中添加一個server,如下所示:
1 # 基於IP的虛擬主機配置2 server {3 listen 80;4 server_name 192.168.80.150; # 新建的虛擬網卡,是內網IP,只能通過 wget訪問5 location / {6 root html/ip;7 index admin.html;8 }9 }
(5) 在nginx/html文件夾中新建 ip文件夾,然後在 ip文件夾中 新建admin.html文件,admin.html文件中的內容為:
Hello,This is IP page.
(6) 重新載入nginx.conf文件,./nginx -s reload
(7) 到nginx/conf 目錄下,訪問 剛剛的IP地址:wget 192.168.80.150,發現下載成功!
