Linux下Nginx配置虛擬機器主機VirtualHost執行個體教程

來源:互聯網
上載者:User

增加 Nginx 虛擬機器主機

這裡假設大家的 Nginx 伺服器已經安裝好, 不懂的請閱讀各 linux 發行版的官方文檔或者 LNMP 的安裝說明. 配置 Virtual host 步驟如下:

進入 /usr/local/nginx/conf/vhost 目錄, 建立虛擬機器主機設定檔 demo.neoease.com.conf ({網域名稱}.conf).

開啟設定檔, 添加服務如下:
 

 代碼如下 複製代碼

server {
    listen          80;       
    server_name     www.topjishu.net;       
    index           index.html index.htm index.php;       
    root            /var/www/www.topjishu.net;        
    log_format      www.topjishu.net '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer' '$http_user_agent $http_x_forwarded_for';       
    access_log      /var/log/www.topjishu.net.log www.topjishu.net;
}


開啟 Nginx 設定檔 /usr/local/nginx/conf/nginx.conf, 在 http 範圍引入虛擬機器主機設定檔如下:

include vhost/*.conf;

重啟 Nginx 服務, 執行以下語句.

service nginx restart

讓 Nginx 虛擬機器主機支援 PHP

在前面第 2 步的虛擬機器主機服務對應的目錄加入對 PHP 的支援, 這裡使用的是 FastCGI, 修改如下.
 

 代碼如下 複製代碼

server {
listen 80;
server_name www.topjishu.net;
index index.html index.htm index.php;
root /var/www/www.topjishu.net;
location ~ .*.(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php; i
nclude fcgi.conf;
}
log_format www.topjishu.net '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for';
access_log /var/log/www.topjishu.net.log www.topjishu.net;
}
 


圖片防盜鏈

圖片作為重要的耗流量大的靜態資源, 可能網站主並不希望其他網站直接引用, Nginx 可以通過 referer 來防止外站盜鏈圖片.

 代碼如下 複製代碼

server {
listen 80;
server_name www.topjishu.net;
index index.html index.htm index.php;
root /var/www/www.topjishu.net; # 這裡為圖片添加為期 1 年的到期時間, 並且禁止 Google, 百度和本站之外的網站引用圖片
location ~ .*.(ico|jpg|jpeg|png|gif)$
{
expires 1y;
valid_referers none blocked demo.neoease.com *.google.com *.baidu.com;
if ($invalid_referer) {
return 404;
}
}
log_format www.topjishu.net '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for';
access_log /var/log/www.topjishu.net.log www.topjishu.net;
}
 


WordPress 偽靜態配置

如果將 WordPress 的連結結構設定為 /%postname%/, /%postname%.html 等格式時, 需要 rewrite URL, WordPress 提供 Apache 的 .htaccess 修改建議, 但沒告知 Nginx 該如何修改. 我們可以將 WordPress 的虛擬機器主機配置修改如下:

 代碼如下 複製代碼

server {
listen 80;
server_name www.topjishu.net;
index index.html index.htm index.php;
root /var/www/www.topjishu.net;
location / {
if (-f $request_filename/index.html){
rewrite (.) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.) $1/index.php;
}
if (!-f $request_filename){
rewrite (.) /index.php;
}
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ ..(php|php5)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
log_format www.topjishu.net '$remote_addr - $remote_user [$time_local] $request' '$status $body_bytes_sent $http_referer ' '$http_user_agent $http_x_forwarded_for'; access_log
/var/log/www.topjishu.net.log demo.neoease.com;
}
 


LNMP 套件在提供了 WordPress 為靜態設定檔 /usr/local/nginx/conf/wordpress.conf, 在虛擬機器主機配置的 server 範圍引用如下即可.

include wordpress.conf;

如果你使用 LNMP 套件, 進入 WordPress 後台發現會出現 404 頁面, wp-admin 後面缺少了斜杆 /, 請在 wordpress.conf 最後添加以下語句:

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

相對 Apache, Nignx 有更加強大的並發能力, 而因為他對進程管理耗用資源也比較少. 而 Apache 比 Nginx 有更多更成熟的可用模組, bug 也比較少.

賣主機的 IDC 選擇 Nignx, 因為高並發允許他們建立更多虛擬機器主機空間更來錢; 淘寶也因此改造 Nignx (Tengine) 作為 CDN 伺服器, 可承受更大壓力.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.