Nginx相关安装、功能开放(主配置文件修改)、状态统计、访问控制虚拟主机
发布日期:2021-05-08 20:55:06 浏览次数:23 分类:精选文章

本文共 3166 字,大约阅读时间需要 10 分钟。

Nginx 网站服务指南

一、编译安装 Nginx

1. 导入安装包

将 Nginx 安装包放置在 /opt 目录下。

2. 关闭防火墙

执行以下命令关闭防火墙:

systemctl stop firewalld && systemctl disable firewalld && setenforce 0

3. 安装依赖包

安装必要的软件包:

yum -y install pcre-devel zlib-devel gcc gcc-c++ make

4. 创建程序用户及组

创建专用用户账号:

useradd -M -s /sbin/nologin nginx

5. 编译安装 Nginx

进入安装目录并配置:

cd /opttar zxvf nginx-1.12.0.tar.gz -C /usr/local/nginxcd /usr/local/nginx./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_modulemake -j 4 && make install

6. 查看 Nginx 进程号

几种方法查询 Nginx 进程号:

cat /usr/local/nginx/logs/nginx.pidnetstat -natp | grep nginxlsof -i :80ps -ef | grep nginx

7. 检查、启动、重启、停止 Nginx 服务

nginx -t  # 检查配置nginx  # 启动cat /usr/local/nginx/logs/nginx.pid  # 查看 PIDkill -3 
# 停止kill -3 -s QUIT
# 平滑停止make -l # 列出所有 Nginx 进程

8. 添加 Nginx 系统服务

创建服务脚本:

vim /etc/init.d/nginxchmod +x /etc/init.d/nginxchkconfig --add nginxsystemctl stop nginx && systemctl start nginx

二、熟悉 Nginx 服务的主配置文件 nginx.conf

1. 全局配置

include mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';access_log logs/access.log main;sendfile on;keepalive_timeout 65;

2. I/O 事件配置

events {    use epoll;    worker_connections 4096;}

3. HTTP 配置

http {    server {        listen 80;        server_name www.kgc.com;        charset utf-8;        location / {            root html;            index index.html index.htm;        }        error_page 500 502 503 504 /50x.html;        location = /50x.html {            root html;        }    }}

4. 地址匹配配置

location /tt {    root /usr/local/nginx/html/tt;    index index.html;}location / {    root html;    index index.html index.htm;}

5. 别名配置

server {    listen 80;    server_name example.com;    location / {        root /var/www/html;        index index.html;    }    location /admin {        alias /var/www/html/admin;        index index.html;    }}

6. 访问状态统计

location /status {    stub_status on;    access_log off;}

三、基于授权的访问控制

1. 生成密码认证文件

htpasswd -c /usr/local/nginx/passwd.db

2. 修改主配置文件

auth_basic "secret";auth_basic_user_file /usr/local/nginx/passwd.db;

3. 测试访问

nginx -t && systemctl restart nginx

四、基于客户端的访问控制

1. 修改主配置文件

deny 192.168.40.30;allow all;

2. 测试访问

访问 http://192.168.40.20 时会被拒绝。

五、虚拟主机配置

1. 基于域名的虚拟主机

server {    listen 80;    server_name www.rain.com;    charset utf-8;    location / {        root /var/www/html/rain;        index index.html index.php;    }    error_page 500 502 503 504 /50x.html;    location = /50x.html {        root html;    }}

2. 基于 IP 的虚拟主机

server {    listen 192.168.40.20:80;    server_name www.rain.com;    charset utf-8;    location / {        root /var/www/html/rain;        index index.html index.php;    }    error_page 500 502 503 504 /50x.html;    location = /50x.html {        root html;    }}

3. 基于端口的虚拟主机

server {    listen 192.168.40.20:8080;    server_name www.rain.com;    charset utf-8;    location / {        root /var/www/html/rain;        index index.html index.php;    }    error_page 500 502 503 504 /50x.html;    location = /50x.html {        root html;    }}
上一篇:数据库简介
下一篇:shell脚本中冒泡排序、直接排序、反转排序

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月18日 01时37分26秒