CentOS 6 搭建 Nginx Web 服务流程
利用 CentOS 6 操作系统构建基础的 Web 服务环境。
注意:本指南未涉及 MySQL、MongoDB 数据库的安装与使用,也不包含 Docker 容器相关配置,主要聚焦于使用 Nginx 作为反向代理来提供静态资源服务。
准备一台运行 CentOS 系统的云服务器。
用户可根据需求选择国内或国外的云服务器提供商。本文仅展示 CentOS 系统的配置步骤,实际操作是在 Windows 环境下进行的远程管理。首先选购一台合适的云服务器,确保操作系统为 CentOS。
在本地 Windows 系统中安装 Xshell 和 Xftp 软件,以确保能够顺利访问和管理 CentOS 系统。
通过 Xshell 连接到云服务器,并使用超级管理员 root 权限进行登录。
成功登录后,开始安装 Nginx 服务。
以下是安装过程中需要下载的相关文件和组件列表:
- 首先执行命令安装 wget 工具:
wget # yum install wget - Nginx 依赖于 gcc 编译环境,需安装 gcc-c++:
# yum install gcc-c++ - Nginx 的 HTTP 模块需要使用 pcre 库来解析正则表达式,因此需安装 pcre 及其开发包:
# yum -y install pcre pcre-devel - 还需要安装相关的解压支持包 zlib 及其开发包:
# yum -y install zlib zlib-devel - 下载 Nginx 的压缩包文件:
# wget -c https://nginx.org/download/nginx-1.10.3.tar.gz
接下来进行解压与安装操作:
- 解压下载的 Nginx 压缩包:
# tar -zxvf nginx-1.10.3.tar.gz - 进入解压后的 Nginx 目录:
#cd nginx-1.10.3 - 对 Nginx 的源码进行编译前的配置检查:
# ./configure - 开始执行编译过程:
# make - 继续完成安装步骤:
# make install - 查看 Nginx 的安装目录,系统将提示 Nginx 的具体位置,其命令通常位于 /usr/local/nginx/sbin 目录下:
# whereis nginx
常用的 Nginx 控制命令如下:
开启 Nginx 服务:# ./nginx
停止 Nginx 服务:# ./nginx -s stop
优雅地停止 Nginx 服务:# ./nginx -s quit
重启 Nginx 服务以应用新配置:# ./nginx -s reload
开启 Nginx # ./nginx 必须在 Nginx 安装目录下的 sbin 文件中执行,当然也可以将其配置为全局命令以便调用。
查看 Nginx 是否正在运行:# ps aux|grep nginx
接下来进行 Nginx 的配置工作。
以下提供部分 Nginx 配置的参考内容。
# http server
server {
listen 80;
server_name www.didiheng.com;
#charset koi8-r;
#access_log logs/host.access.log main;
access_log off; #缓存日志关闭
server_tokens off;
tcp_nopush on;
tcp_nodelay on;
gzip on; #gzip开启
gzip_comp_level 6; #gzip比率
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
proxy_connect_timeout 5; #缓存链接
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
location / {
root /www; #此处绝对地址
index index.html index.htm;
try_files $uri $uri/ /index.html; //使用客户端路由需配置
rewrite ^(.*)$ https://$host$1 permanent; /强制定向https
}
#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;
}
#https server
server {
listen 443 ssl;
server_name www.didiheng.com;
ssl_certificate server.crt;
ssl_certificate_key server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
access_log off;
server_tokens off;
tcp_nopush on;
tcp_nodelay on;
expires epoch;
gzip on;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
location / {
root /www;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
}
在上述配置示例中,我直接开启了 HTTPS 支持。如果没有相关的 SSL 证书配置,请使用 # 符号注释掉 https 服务相关行。
修改配置文件后,需要重启 Nginx 使更改生效:
# ./nginx -s reload
使用服务器 IP 地址或绑定的域名进行访问测试。
GitHub 原项目地址。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。