可锐资源网

技术资源分享平台,提供编程学习、网站建设、脚本开发教程

【入门】CentOS 7 系统上安装 Nginx后做配置(踩坑)

接上文:

【入门】CentOS 7 系统上安装 Nginx

安装Nginx成功后,需要了解和实现:

1、nginx安装地址

2、配置文件在哪里

3、怎么配置应用

参考原先的笔记:

我在自己腾讯云服务器上更改nginx配置的操作指引

一、找到nginx的部署位置

which nginx

返回:/usr/sbin/nginx


意义不大,只是知道nginx的执行文件位置

然后

ps -ef | grep nginx

得到:

[root@localhost fuzhx]# ps -ef | grep nginx
root       2609      1  0 00:24 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      2610   2609  0 00:24 ?        00:00:00 nginx: worker process
root       2776   2477  0 00:28 pts/0    00:00:00 grep --color=auto nginx


配置文件:

/etc/nginx/nginx.conf

查看一下配置文件的内容

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

看到有:

include /etc/nginx/conf.d/*.conf;

然后在查看一下:/etc/nginx/conf.d 目录


有一个default.conf

查看内容:

server {
    listen       80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

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

    #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   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

location那里设置root /usr/share/nginx/html

现在,我要尝试几个设置:

1、当前服务器部署新的站点,用80端口,不同的server_name(80.p133.lookdaima.com)

2、当前服务器部署新的站点,用81端口,用localhost(需要开放防火墙)

3、用81端口,反向代理访问外部网站

4、当前服务器部署新的站点,用8089端口,不同的server_name

(www.p133.lookdaima.com)(需要开放防火墙,安全设置)

其中,第四点就是我昨晚搞了很久的踩坑环节,差点自闭了。

现在说重点:

提前说明几个命令:

# 检查配置
sudo nginx -t
# 重启
sudo systemctl restart nginx
# 查看状态
sudo systemctl status nginx

1、当前服务器部署新的站点,用80端口,不同的server_name

约定server_name:80.p133.lookdaima.com

先参考:/usr/share/nginx/html

设置html-two站点目录和文件

然后在/etc/nginx/conf.d目录,创建一个80p133.conf

server {
    listen 80;
    server_name 80.p133.lookdaima.com;

    location / {
        root   /usr/share/nginx/html-two;
        index  index.html index.htm;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html-two; 
    }
}

然后

# 检查配置(可选)
sudo nginx -t
# 重启
sudo systemctl restart nginx
# 查看状态(可选)
sudo systemctl status nginx

访问:
http://80.p133.lookdaima.com 验证效果


配置成功了

2、当前服务器部署新的站点,用81端口,用localhost

然后在/etc/nginx/conf.d目录,创建一个81local.conf

server {
    listen 81;
    server_name localhost;

    location / {
        root   /usr/share/nginx/html-two;
        index  index.html index.htm;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html-two; 
    }
}

然后

# 检查配置(可选)
sudo nginx -t
# 重启
sudo systemctl restart nginx
# 查看状态(可选)
sudo systemctl status nginx


访问:
http://192.168.203.133:81/,

访问失败,需要开启防火墙



# 1. 开放 81 端口(永久生效)
sudo firewall-cmd --permanent --zone=public --add-port=81/tcp  
# 2. 重载防火墙规则(让配置生效)
sudo firewall-cmd --reload  
# 3. 验证端口是否开放
sudo firewall-cmd --zone=public --list-ports   
# 或
sudo firewall-cmd --zone=public --list-ports | grep 81 


再次验证:


成功

3、用81端口,反向代理访问外部网站

修改81local.conf

server {
    listen 81;
    server_name localhost;

    location / {
        proxy_pass http://www.lookdaima.com; 
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html-two; 
    }
}


成功

4、当前服务器部署新的站点,用8089端口,不同的server_name

(www.p133.lookdaima.com)(需要开放防火墙,安全设置)

在/etc/nginx/conf.d目录,创建一个p8089.conf

server {
    listen 8089;
    server_name www.p133.lookdaima.com;

    location / {
        root   /usr/share/nginx/html-three;
        index  index.html index.htm;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html-three; 
    }
}

启动报错:

[root@localhost conf.d]# sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost conf.d]# sudo systemctl restart nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@localhost conf.d]#


 journalctl -xe
If you want to allow nis to enabled
Then you must tell SELinux about this by enabli

AI查询原因

SELinux(Security-Enhanced Linux ,安全增强型 Linux )是美国国家安全局(NSA)主导开发的 Linux 内核强制访问控制(MAC)安全子系统,为系统资源提供更精细、严格的访问管控,以下从核心要点展开介绍:

一、工作原理与模型

基于域 - 类型(domain-type) 强制访问控制模型,进程运行在特定 “域(domain)” ,文件、端口等资源有 “类型(type)” ,系统通过策略规则,限制域对不同类型资源的访问(如 HTTP 服务进程域,仅能访问自身类型标记的文件、端口 ),弥补传统 UNIX 自主访问控制(DAC,靠用户 / 组权限管控)的不足,即便进程被入侵,也难越权访问其他资源。

二、运行模式

禁用(disabled)、容许(permissive)、强制(enforcing) 三种:



  • 禁用:完全关闭 SELinux,不做访问检查 / 日志记录。
  • 容许:SELinux 生效,违规操作会记录日志(/var/log/audit/audit.log 等),但仍允许操作,适合调试策略。
  • 强制:严格执行策略,违规直接阻止操作,保障系统安全。



可通过 getenforce 查看当前模式,setenforce 0/1(0 切换为容许,1 切换为强制,重启后失效 )临时切换;永久修改需改 /etc/sysconfig/selinux(或 /etc/selinux/config )里的 SELINUX 字段。

三、关键作用与优势

  • 最小权限管控:限制进程仅能访问必要资源,比如 Web 服务进程无法随意访问数据库配置文件,降低入侵后横向渗透风险。
  • 增强系统韧性:即便系统服务(如 Apache、MySQL )存在漏洞被利用,因 SELinux 策略限制,攻击者也难获取高权限或破坏其他关键资源。
  • 日志追溯:容许 / 强制模式下,违规操作会记录详细日志,便于排查安全事件。

四、常见操作与工具

  • 查看上下文(context):ls -Z 查看文件 / 目录的 SELinux 上下文;ps -eZ 查看进程的域信息。
  • 修改上下文:chcon 临时修改文件上下文(重启 /relabel 后可能失效 );restorecon 恢复文件默认上下文(依赖策略 );semanage 可永久修改(需安装 policycoreutils-python 等工具包 )。
  • 策略管理:seinfo 查看策略规则;sesearch 搜索策略;自定义策略可借助 audit2allow ,将审计日志转化为策略模块加载。

五、与 Nginx 等服务的关联

以 Nginx 访问异常为例,若 SELinux 处于强制模式,且 Nginx 需访问的资源(如网页目录、日志文件 )上下文配置不当,可能触发 SELinux 策略拦截,导致访问失败。可通过:


  1. grep nginx /var/log/audit/audit.log 查看 Nginx 相关的 SELinux 违规日志。
  2. 用 audit2allow -M mynginx < audit.log 生成允许 Nginx 操作的策略模块,再 semodule -i mynginx.pp 加载,或临时切换到容许模式(setenforce 0 )测试是否因 SELinux 限制导致问题。

简单说,SELinux 是 Linux 系统的 “安全门卫”,通过严格策略管控进程与资源的访问,虽增加配置复杂度,但能显著提升系统安全性,尤其在生产环境中,合理配置可有效抵御各类渗透风险 。


首先,我开启防火墙,避免防火墙问题

#防火墙
sudo firewall-cmd --permanent --zone=public --add-port=8089/tcp
sudo firewall-cmd --reload  

sudo setenforce 0  # 临时关闭
sudo systemctl restart nginx  # 尝试重启 Nginx


成功,证明是SELinux的问题

更改策略

# 允许 Nginx 使用 8089 端口
sudo semanage port -a -t http_port_t -p tcp 8089

重启系统,试一下效果


重启后验证,可以正常使用了。

补充说明:

昨天我安装好nginx后,因为太晚,大脑没有好好思考,各种尝试都失败,有conf文件写错后缀config等,一直没有实现预期效果。

踩了一圈坑后,从简单到复杂,慢慢把问题理清楚了。

笔记如上。

总结:

# 配置目录
/etc/nginx/conf.d

# 防火墙开通
# 1. 开放 81 端口(永久生效)
sudo firewall-cmd --permanent --zone=public --add-port=81/tcp  
# 2. 重载防火墙规则(让配置生效)
sudo firewall-cmd --reload  
# 3. 验证端口是否开放
sudo firewall-cmd --zone=public --list-ports   
# 或
sudo firewall-cmd --zone=public --list-ports | grep 81 

# 如果大于1024的端口,需要
# 允许 Nginx 使用 8089 端口
sudo semanage port -a -t http_port_t -p tcp 8089

# 重启
# 检查配置(可选)
sudo nginx -t
# 重启
sudo systemctl restart nginx
# 查看状态(可选)
sudo systemctl status nginx
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言