Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> Web 伺服器

Linux 手把手編譯安裝 Nginx 並配置多站點、自訂 404 與常見錯誤排查

編輯:Web 伺服器

一、環境準備與依賴安裝

# CentOS/RHEL
yum -y install gcc gcc-c++ autoconf automake make \
zlib zlib-devel openssl openssl-devel pcre-devel

# 建立專用使用者/群組
groupadd -r nginx
useradd -s /sbin/nologin -g nginx -r nginx

二、下載、編譯、安裝

wget https://nginx.org/download/nginx-1.5.2.tar.gz
tar -zxvf nginx-1.5.2.tar.gz
cd nginx-1.5.2

./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--with-http_stub_status_module

make && make install

提示:若 make 失敗提示缺少編譯器,執行 yum -y install gcc automake autoconf libtool make。

三、啟動、重載、測試

# 啟動
/usr/local/nginx/sbin/nginx

# 重載配置(平滑重啟)
/usr/local/nginx/sbin/nginx -s reload

# 測試配置語法
/usr/local/nginx/sbin/nginx -t

四、多站點(虛擬主機)配置

  1. 建立 vhost 目錄:mkdir /usr/local/nginx/conf/vhost
  2. 各站點建立獨立 .conf,如 linlik.conf:
server {
    listen 80;
    server_name jiahaolin.com www.111cn.net;
    index index.html index.htm index.php;
    root /www/jiahaolin;
    include emlog.conf;

    location ~ [^/]\.php(/|$) {
        try_files $uri =404;
        fastcgi_pass unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; }
    location ~ .*\.(js|css)?$ { expires 12h; }

    access_log /home/wwwlogs/jiahaolin.com.log access;
}
  1. 在 nginx.conf http 區塊加入:include vhost/*.conf;
  2. 重載 Nginx 生效。

五、自訂 404/50x 錯誤頁面

  1. 建立 404.html(>512 bytes,避免 IE 內建頁面)。
  2. nginx.conf http 區塊加入:fastcgi_intercept_errors on;
  3. 站點 server 區塊加入:error_page 404 /404.html; 或 error_page 404 =https://www.xxx.com/404.html;
  4. 同理可設定 error_page 500 502 503 504 /50x.html;
  5. 重載並測試。

關鍵:缺少 fastcgi_intercept_errors on; 會導致自訂錯誤頁不生效。

六、常見錯誤與解決

1. bind() to 0.0.0.0:80 failed (98: Address already in use)

# 找出佔用 80 埠的進程
lsof -i :80
# 或
netstat -anp | grep :80

# 強制終止
kill -9 
# 或
killall -9 nginx

# 重新啟動
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

2. worker_connections > open file resource limit

# 臨時生效
ulimit -n 65535

# 永久生效 /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
# 需重新登入或重啟

3. error_log off 不會關閉日誌

正確關閉:error_log /dev/null crit;

4. 權限導致錯誤日誌無法寫入

確保 Nginx 執行使用者對日誌目錄有寫入權限,或調整 error_log 路徑。

七、最佳實務小結

  • 配置變更後務必 nginx -t 再 nginx -s reload。
  • 生產環境建議啟用 access_log 與 error_log,設定 logrotate 定期輪轉。
  • 使用 include 模組化管理虛擬主機,便於維護。
  • 關注官方安全公告,適時升級版本。
Copyright © Windows教程網 All Rights Reserved