1. 程式人生 > >Nginx下配置虛擬主機!詳細的那種!

Nginx下配置虛擬主機!詳細的那種!

1,先以root使用者登入。找到/usr/local/nginx (預設安裝到此處!) 在這裡插入圖片描述 sbin :存放二進位制啟動檔案。往後每次進行配置變更後,都要來此處進行重啟生效。 HTML:存放網頁。有點了解應該都懂! 但是我們要在conf裡進行配置。哈哈 在這裡插入圖片描述 2,進入conf目錄。開啟nginx.conf檔案。進行編輯配置! 在這裡插入圖片描述 開啟後,如圖: 縮小了好多。便於整體瀏覽: 在這裡插入圖片描述 帶#符號的行都是解釋內容。 #user nobody; #配置允許執行nginx伺服器的使用者和使用者組 worker_processes 1;#配置允許nginx程序生產的worker process數

#error_log logs/error.log; #配置nginx伺服器執行對錯誤日誌存放路徑 #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 - remoteuser[remote_user [time_local] “KaTeX parse error: Expected 'EOF', got '#' at position 16: request" ' #̲ …

status bodybytessent"body_bytes_sent "http_referer” ’ # ‘“httpuseragent""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; #} server { listen 80; server_name 192.168.158.100; location / { root /yuanlai; index index.html index.htm; } } # 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; # } #} } 在最後一個大括號之前加上你要新增的虛擬IP。

server { listen 80; server_name 192.168.158.100; location / { root /yuanlai; index index.html index.htm; } } } 在這裡插入圖片描述 在重啟Nginx時如果報錯的話。一般都是大括號的事情!慢慢理理就可以找出來啦!

3,在ifconfig里加上與上面的另外的IP對應的:

ifconfig ens33:0 192.168.158.100 netmask 255.255.255.0 up ens33是預設的!必須查詢一下,這樣才可以確保! 在這裡插入圖片描述

加上之後會發生改變: 在這裡插入圖片描述 3,關掉防火牆: 在這裡插入圖片描述 4,來驗證一下吧! 192.168.158.134:第一個IP。 在這裡插入圖片描述 目錄是HTML。就是Nginx下的。照片裡有路徑: 在這裡插入圖片描述 第二個:192.168.158.100照片裡也有路徑: 在這裡插入圖片描述 在這裡插入圖片描述 驗證: 192.168.158.134 在這裡插入圖片描述 192.168.158.100 在這裡插入圖片描述 OK!