LNMP 架构指的是 Linux、Nginx、MySQL 和 PHP 这四种技术的组合,常用于搭建网站服务器。在 LNMP 架构中,Nginx 作为 Web 服务器,负责处理 HTTP 请求。以下是 Nginx 的基本配置步骤和配置文件结构:
Nginx 安装步骤
- 安装编译工具及库文件:
yum -y install make automake autoconf libtool zlib zlib-devel gcc gcc-c++ openssl openssl-devel
- 安装 PCRE(目的:让 Nginx 支持 rewrite 功能):
cd /usr/local/src/ wget http://ftp.pcre.org/pub/pcre/pcre-8.35.tar.gz tar zxvf pcre-8.35.tar.gz cd pcre-8.35 ./configure make && make install pcre-config --version
- 安装 Nginx:
cd /usr/local/src/ wget http://nginx.org/download/nginx-1.14.0.tar.gz tar zxvf nginx-1.14.0.tar.gz cd nginx-1.14.0 ./configure --prefix=/usr/local/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --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 \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_stub_status_module \ --with-http_auth_request_module \ --with-mail \ --with-mail_ssl_module \ --with-file-aio \ --with-threads \ --with-stream \ --with-stream_ssl_module \ --with-ipv6 make && make install
Nginx 配置文件结构
Nginx 配置文件(通常命名为 nginx.conf)位于 Nginx 安装目录的 conf 目录下,其整体结构如下:
- 全局块:包含 Nginx 的全局配置,如运行用户、worker 进程数、日志配置等。
- events 块:配置 Nginx 的事件模型,如 worker 进程如何处理请求、如何处理网络连接等。
- http 块:Nginx 配置文件的核心部分,用于配置 HTTP 服务器,包含 HTTP 请求的处理方式、虚拟主机的配置等。
- server 块:用于配置 Nginx 的虚拟主机,每个 server 块表示一个虚拟主机,包含该虚拟主机的各种配置指令。
- location 块:用于配置 Nginx 的 URL 路由,表示匹配某个 URL 请求时所需执行的操作。
Nginx 配置文件示例
以下是一个简单的 Nginx 配置文件示例:
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } }
在配置 Nginx 时,通常需要根据实际需求调整上述配置,例如设置监听端口、服务器名称、网站根目录等。此外,还可能需要配置 HTTPS、负载均衡、缓存等功能,这些高级配置超出了基本配置的范围,但可以通过 Nginx 的官方文档和相关教程来学习。