Nginx安装与配置(包括解决403错误,站点配置,_STORAGE_WRITE_ERROR_,No input file)

本文介绍了Nginx的安装过程,并针对遇到的403权限错误提供了排查方法,包括检查文件权限和阅读错误日志。同时,讲解了如何在Nginx中部署TP框架,以及解决_NO_INPUT_FILE_和_STORAGE_WRITE_ERROR_问题,重点是确保Runtime目录的读写权限设置正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.yum 安装nginx
nginx -v   #安装前检查
yum install nginx -y  #yum安装nginx
2.启动nginx并设置开机自启动
 
systemctl start nginx.service  #启动nginx
 systemctl stop  nginx.service  #关闭nginx
 systemctl enable nginx.service #设置nginx开机自启
 systemctl status nginx.service #查看nginx运行状态
3.安装完成后即可输入ip进行访问,若出现403,检查是否关闭防火墙
 
systemctl stop firewalld  #关闭防火墙
4.修改nginx配置文件 使其支持php解析,并配置相关参数

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;  #运行用户
worker_processes auto;  ##启动进程,通常设置成和cpu的数量相等
error_log /var/log/nginx/error.log; #错误日志位置
pid /run/nginx.pid; #全局错误日志及PID文件

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
worker_rlimit_nofile 51200; #单个后台worker process进程的最大并发链接数

events {
    use epoll;  #仅用于linux2.6以上内核,可以大大提高nginx的性能
    worker_connections 51200;
    multi_accept on;
}

http {
    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;
    charset             utf-8;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        index index.html index.htm index.php;
       # root         /usr/share/nginx/html;    #默认网站根目录
        root          /home/www;    #自定义网站根目录 需要给足读写权限
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        index index.html index.htm index.php;
        autoindex on;
        }
        location ~ \.php$ {
         #root /usr/share/nginx/html; #指定php的根目录
         root /home/www;    #自定义网站根目录 需要给足读写权限
         fastcgi_pass 127.0.0.1:9000;#php-fpm的默认端口是9000
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }
  include vhosts/*.conf;
}

5.启动php-fpm(有两种启动方式,1.手动切换到目录下启动,2.设置开机自启动)
  a.手动启动:切换到php-fpm目录 执行./php-fpm     </usr/sbin/php-fpm>
  b.开机自启:systemctl enable php-fpm.service 

6.关闭 selinux(selinux开启可能会导致访问时出现403)  
  a. 临时关闭  
setenforce 0

  b. 永久关闭

vi /etc/selinux/config(find / -name selinux)
  将SELINUX=enforcing改为SELINUX=disabled 
  设置后需要重启才能生效

7.配置虚拟站点
a.find / -name nginx.conf #找到nginx.conf目录路径 </etc/nginx/nginx.conf>
b.在nginx目录下新建文件夹vhosts
c.切换到vhosts目录,新建web1.conf,web1.conf配置如下
server {
    listen       80;
    server_name  web1.com;
    root        /home/www/web/;
    index  index.php index.html;


    location / {
        index  index.php index.html;
    }
    location ~ .*\.(php|php5)?$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        fastcgi_param APPLICATION_ENV development;
        include fastcgi_params;
    }
}
d.并在nginx.conf的server最后添加  include vhosts/*.conf;
e.配置解析,find / -name hosts </etc/hosts> 添加域名及ip,还需在本机hosts中也添加ip地址

8.如果nginx安装或运行过程中出现 403 错误  首先检查是否具有读写权限,如果出现其他错误,可使用cat命令查看错误日志进行问题排查 </var/log/nginx/error.log>

9.部署tp框架在Nginx中(或者出现 No input file)

使Nginx支持tp框架支持:common中加入以下配置

return array(
	'URL_MODEL' => '2', //URL模式 或者 'URL_MODEL' =>3(URL兼容模式)
	'URL_PATHINFO_FETCH' => ':get_path_info', //加入此项配置
);
Nginx.conf:
location / { // …..省略部分代码
   if (!-e $request_filename) {
   rewrite  ^(.*)$  /index.php?s=$1  last;
   break;
    }
 }

如出现下图情况(_STORAGE_WRITE_ERROR_),请检查Runtime目录是否具有读写权限


### 项目概述 一个基于 Python 的分布式文件存储系统可以通过多种方式实现。可以使用对象存储服务(如 AWS S3)来构建,也可以通过自定义的分布式架构实现。为了演示该项目,我们将构建一个简单的分布式文件存储系统,并展示其基本功能。 --- ### 系统设计核心组件 #### 1. 文件分片 将大文件分割为多个小块(称为“分片”),以便在多个节点上并行存储和处理。这提高了系统的扩展性和容错能力[^1]。 ```python def split_file(file_path, chunk_size=1024*1024): """将文件分割为多个分片""" chunks = [] with open(file_path, 'rb') as f: index = 0 while True: chunk = f.read(chunk_size) if not chunk: break chunk_path = f"{file_path}.part{index}" with open(chunk_path, 'wb') as cf: cf.write(chunk) chunks.append(chunk_path) index += 1 return chunks ``` #### 2. 分布式节点 每个节点负责存储一部分数据,并提供上传、下载和删除接口。这里我们使用 Flask 框架创建一个简单的 REST API 来模拟存储节点。 ```python from flask import Flask, request, jsonify import os app = Flask(__name__) STORAGE_DIR = "storage" os.makedirs(STORAGE_DIR, exist_ok=True) @app.route('/upload', methods=['POST']) def upload(): file = request.files['file'] file.save(os.path.join(STORAGE_DIR, file.filename)) return jsonify({"status": "success"}) @app.route('/download/<filename>', methods=['GET']) def download(filename): path = os.path.join(STORAGE_DIR, filename) if not os.path.exists(path): return jsonify({"error": "File not found"}), 404 with open(path, 'rb') as f: data = f.read() return data @app.route('/delete/<filename>', methods=['DELETE']) def delete(filename): path = os.path.join(STORAGE_DIR, filename) if os.path.exists(path): os.remove(path) return jsonify({"status": "deleted"}) else: return jsonify({"error": "File not found"}), 404 if __name__ == '__main__': app.run(port=5000) ``` #### 3. 客户端操作 客户端负责协调文件的上传、下载以及从多个节点中获取完整的文件。 ```python import requests import os NODES = ["http://localhost:5000"] # 可以扩展为多个节点地址 def upload_file_to_node(file_path, node_url): with open(file_path, 'rb') as f: files = {'file': f} response = requests.post(f"{node_url}/upload", files=files) return response.json() def download_file_from_node(filename, node_url, save_path): response = requests.get(f"{node_url}/download/{filename}") if response.status_code == 200: with open(save_path, 'wb') as f: f.write(response.content) return True else: return False # 示例:上传文件到第一个节点 upload_file_to_node("example.txt", NODES[0]) ``` --- ### 运行步骤 #### 1. 启动节点服务器 打开终端,运行以下命令启动一个本地存储节点: ```bash python node_server.py ``` #### 2. 上传文件 执行客户端脚本上传文件到节点: ```bash python client.py ``` #### 3. 验证文件存在 访问 `http://localhost:5000/download/example.txt` 或检查 `storage/` 目录下的文件内容。 --- ### 展示方案 #### 1. 用户界面 可以使用 Flask + HTML 构建一个简单的 Web 界面,让用户上传、查看和下载文件。 ```html <!-- templates/index.html --> <!DOCTYPE html> <html> <head> <title>Distributed File Storage</title> </head> <body> <h1>Upload a File</h1> <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Upload"> </form> <h2>Files Available</h2> <ul> {% for file in files %} <li><a href="/download/{{ file }}">{{ file }}</a></li> {% endfor %} </ul> </body> </html> ``` #### 2. 多节点部署 可以使用 Docker 将每个节点打包成容器,并通过 Kubernetes 或 Docker Compose 实现多节点部署,从而展示系统的分布式特性。 ```yaml # docker-compose.yml version: '3' services: storage-node-1: build: . ports: - "5000:5000" storage-node-2: build: . ports: - "5001:5000" ``` --- ### 扩展功能建议 - **一致性哈希**:用于更高效地分配文件到不同节点。 - **冗余备份**:每个文件分片可以在多个节点上备份,防止数据丢失。 - **加密传输**:确保文件在传输过程中的安全性。 - **负载均衡**:使用 Nginx 或 HAProxy 分发请求到不同的存储节点。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值