社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  NGINX

Cherrypy NGINX错误:403/some/path目录索引被禁止

Cameron Yee • 6 年前 • 596 次点击  

我在Ubuntu 18.04x64数字海洋服务器上运行NGINX。我有一个Cherrypy应用程序直接在Ubuntu服务器上运行。我正在尝试使用NGINX代理到我的Cherrypy应用程序的特定路径。代理通行证似乎正在工作,但当我尝试投递到路由时,收到了403个禁止错误。Cherrypy路由在本地测试Python请求POST请求时可以工作,但在使用proxy_pass通过NGINX发送请求时不起作用。

Cherrypy在Pipenv虚拟环境中。为了运行它我运行 Python3 app.py .

2019/10/17 20:51:50 [error] 29574#29574: *51 directory index of "/mnt/media_storage/media_root/media/monday/monday-file-upload/" is forbidden, client: 73.14.140.118, server: media.bscs.org, request: "GET /monday/monday-file-upload/ HTTP/1.1", host: "media.bscs.org"

这是我的NGINX配置:

# Microcaching
proxy_cache_path /tmp/cache keys_zone=cache:10m levels=1:2 inactive=600s max_size=100m;

# Cache in browser
# Expires map
map $sent_http_content_type $expires {
    default                     off;
    text/html                   epoch;
    text/css                    30d;
    application/javascript      30d;
    ~image/                     30d;
}

upstream apps {
    server 127.0.0.1:8080;
}

server {
  listen 80;
  listen [::]:80;
  server_name media.bscs.org;
  rewrite ^/(.*) https://media.bscs.org/$1 permanent;
}

server {
  listen *:443 ssl http2;
  listen [::]:443 ssl http2;
  server_name media.bscs.org;

  root /mnt/media_storage/media_root/media;
  charset utf-8;

  client_max_body_size 1000M;

  # Gzip/compress text-based assets
  gzip on;
  gzip_http_version 1.0;
  gzip_vary on;
  gzip_comp_level 6;
  gzip_proxied any;
  gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml text/javascript application/javascript image/svg+xml;
  gzip_disable "MSIE [1-6]\.";

  # make sure gzip does not lose large gzipped js or css files
  # see http://blog.leetsoft.com/2007/7/25/nginx-gzip-ssl
  gzip_buffers 16 8k;

  # Microcaching
  proxy_cache cache;
  proxy_cache_valid 200 1s;

  # Cache in browser
  expires $expires;

  ssl on;
  ssl_ciphers "my-cipher";
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;

  ssl_session_cache shared:SSL:10m;

  add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";


  add_header X-Content-Type-Options nosniff;
  add_header 'Access-Control-Allow-Origin' '*';

  ssl_session_tickets off;
  ssl_stapling on;
  ssl_stapling_verify on;
  resolver_timeout 5s;
  ssl_certificate /etc/nginx/ssl/cert_chain.crt;
  ssl_certificate_key /etc/nginx/ssl/STAR.bscs.org.key;
  ssl_dhparam /etc/nginx/ssl/dhparam.pem;

  location = /favicon.ico {
    access_log off;
    log_not_found off;
    sendfile on;
    sendfile_max_chunk 1m;
  }

  location ~* \.(gif|jpg|jpeg|png|js|css)$ {
        log_not_found off;
        access_log off;
        sendfile on;
        sendfile_max_chunk 1m;
  }

  location /media/ {
    alias /mnt/media_storage/media_root/media/;

    location /media/monday/monday-file-upload/ {
      alias /mnt/media_storage/media_root/media/monday/monday-file-upload/;

      proxy_pass        http://apps/;
      proxy_redirect    off;
      proxy_set_header  Host $host;
      proxy_set_header  X-Real-IP $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header  X-Forwarded-Host $server_name;
    }
  }

  # Redirects
  rewrite ^/tech-report/2018-1/2018-1.html$ https://bscs.org/resources/reports/designing-citizen-science-for-both-science-and-education-a-workshop-report/$1 permanent;

}

def uploadFileToMediaServer(uploaded_file_local_path):
  with open(uploaded_file_local_path, 'rb') as f:
      files = {'uploaded_file': f}

      r = requests.post('https://media.bscs.org/monday/monday-file-upload', files=files)
      print(r.request.url, file=sys.stderr)
      print(r.request.headers, file=sys.stderr)

      return r

这是我的Cherrypy应用程序:

import cherrypy
from cherrypy.process.plugins import Daemonizer

config = {
    'global': {
        'server.socket_host': '127.0.0.1',
        'server.socket_port': 8080,
        'server.thread_pool': 8,
        'server.max_request_body_size': 0,
        'server.socket_timeout': 60
    }
}

class App:
    @cherrypy.expose
    def index(self, uploaded_file):
        try:
            with open('../uploads/{}'.format(uploaded_file.filename), 'wb') as f:
                while True:
                    data = uploaded_file.file.read(8192)
                    if not data:
                        return {'message': 'File failed to upload'}
                    f.write(data)

            return {'message': 'File uploaded successfully'}
        except Exception:
            cherrypy.log(Exception, traceback=True)


if __name__ == '__main__':
    d = Daemonizer(cherrypy.engine)
    d.subscribe()

    cherrypy.tree.mount(App(), "/", config)
    cherrypy.engine.start()
    cherrypy.engine.block()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54957