nginx虛擬主機配置


新版本nginx的配置文件被拆分為若干部分

1、主配置文件為nginx.conf

2、與php相關的是fastcgi_params

3、與python相關的是uwsgi_params

4、...其他配置文件

[PS:首先確保占用80端口的服務被停止,nginx默認監聽端口為80]

我們首先可以打開nginx.conf

 nginx.conf主配置文件
#使用的用戶和組
#user nobody;
#指定工作衍生進程數(默認為CPU的線程數!)
worker_processes 2;
#指定pid存放的路徑,應該記錄了nginx守護進程的進程號,是其他進程的父進程id!(如Apache下有PidFile )
pid /var/run/nginx.pid;
events {
use epoll;#linux下性能最好的event模式!
#允許的連接數
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 測試多次請求傳輸之間的時間,再一次持久鏈接,如果客戶端的兩次HTTP請求超過keepalive_timeout的時間,則關閉鏈接釋放資源!!(Apache下也有)
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
#
default_type application/octet-stream;
#當用戶請求的文件木有在服務器中定義mime類型映射,使用DefaultType
#可以有啥? text/html text/plain(表示后綴為txt的文件以文本顯示) image/gif
#這里的application/octet-stream 是啥?
#若未定的文件多是軟件或者圖像,建議為application/octet-stream
#瀏覽器會提示用戶以“另存為”的方式保存文件!
##
# Logging Settings
##
#日志,詳細見后面
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##開啟gzip壓縮
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
##很好,我們可以清楚的看到,我們會加載conf.d目錄與sites-enabled目錄下得所有文件,這也證明了我們為啥子可以在這兩個目錄下寫配置文件!
}
#nginx同樣可以做郵件代理服務器!
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}

 nginx的虛擬主機配置

如下圖是最簡單的虛擬主機配置文件

 

與Apache相同,nginx也可以配置多種類型的虛擬主機。

  • 基於域名的虛擬主機
  • 基於IP的虛擬主機
  • 基於端口的虛擬主機

然后我們發現目錄下有site-available與site-enabled兩個目錄,和Apache一模一樣,我們一般采用的方法是在前者下寫好配置文件,到后者目錄下做好一個軟連接!原因如同目錄的名字一樣,前者是存在的網站,而后者是正在使用的目錄!nginx默認會加載site-enabled目錄!前者的目錄下有一個default給我們參考如何寫虛擬主機的配置文件

讓我們來看一下這段:

# You may add here your
# server {
# ...
# }
# statements for each of your virtual hosts to this file
#這段文字很清晰告訴我們如何添加虛擬主機!
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
##上面這段講了具體可以參考那些網站和例子!
server {
#開始了直接server打頭,如果配置在nginx.conf里,請寫在http{……server{……}}里,咱們是基於http協議的!
#listen 80; ## listen for ipv4; this line is default and implied
#監聽80端口!
#listen [::]:80 default ipv6only=on; ## listen for ipv6
#root HTML網頁文件存放的目錄
root /usr/share/nginx/www;
#默認首頁文件,順序從左往右!如果找不到index.html,就去找index.htm`
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
#主機名稱
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
}
#顯而易見,訪問根目錄,直接跳轉到首頁!
location /doc {
root /usr/share;
autoindex on;#
allow 127.0.0.1;
deny all;
}
location /images {
root /usr/share;
autoindex off;
}
#這里涉及了404,50x的頁面及其地址
#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 /usr/share/nginx/www;
#}
# 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$ {
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# 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;
# root html;
# index index.html index.htm;
#
# location / {
# try_files $uri $uri/ /index.html;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
#
# root html;
# index index.html index.htm;
#
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
#
# ssl_session_timeout 5m;
#
# ssl_protocols SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
# ssl_prefer_server_ciphers on;
#
# location / {
# try_files $uri $uri/ /index.html;
# }
#}

我們可以發現要配多個虛擬主機基本的格式是

server{……}

server{……}

虛擬主機建立的方式共分為三種:基於IP的虛擬主機,基於端口的虛擬主機和基於名稱的虛擬主機。前兩種由於受到成本和客戶使用習慣的限制,相對使用的沒有基於名稱的虛擬主機多。

1、基於主機名稱的虛擬機配置
server{
 listen 80;
 server_name www.example.com
}
server{
listen 80;
server_name www.test.com
} 
server{
 listen 80 default_server;
  ...
}

  上述配置中, 定義了三個虛擬主機。前兩個 server, 通過域名“www.example.com” 和 “www.test.com” 可以分別訪問正確的網站。如果瀏覽器直接通過 IP 地址或者其他指向這台機器的域名訪問, 那么訪問到的是第三個 server 配置。第三個 server 為一個默認配置, 請注意它沒有“server_name”指令, 並且“listen”指令包含一個“default_server”關鍵字。

2、基於IP的虛擬主機
server{
listen 10.0.0.88:80; root 88.com;
index index.html;
}
server{
listen 10.0.0.87:80;
root 87.com;
index index.html;
}

[PS:請自行分配相應IP地址,並建立88.com與87.com目錄]  

以上配置了兩台虛擬主機,一台 IP 為 10.0.0.88,另一台為 10.0.0.87。它們都監聽 80端口。根據訪問的 IP 地址不同,返回不同網站內容。

3、基於端口的虛擬主機
server{
 listen 80;
 root 80.com; 
}
server{
 listen 8080;
 root 8080.com;
}

以上配置了兩台虛擬主機,一台使用相同 IP。一台使用 80 端口,另一台使用 8080 端口。訪問 8080 端口時需要在 URL 后加上 :8080 。


免責聲明!

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



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