502网关由于权限被拒绝而损坏

时间:2020-02-09 18:07:23

标签: django nginx gunicorn

我正在尝试使用nginx和gunicorn在digitalocean上部署django项目。 我的项目具有以下结构

projects
|_isli
  |_isli
    |_forecast #project directory
      |_manage.py
      |_forecast.sock
      |_forecast
        |_wsgi.py
        |_settings.py
        |_urls.py

我的项目在根目录中创建,而没有创建其他sudo用户。我知道这不是正确的解决方案,但我决定这样做。 在允许主机内的settings.py文件中,我指定了IP地址

ALLOWED_HOSTS = ['165.22.23.233']

在digitalocean官方文档中,有关于使用nginx和gunicorn Deploying django using Nginx and Gunicorn部署django的教程

本文中使用的方法中,将Gunicorn设置为socet的方法是我的设置/etc/systemd/system/gunicorn.service

[Unit]                                                        
Description=gunicorn daemon                                   
After=network.target


[Service]                                                     
User=root                                                   
Group=root                                                  
WorkingDirectory=/root/projects/isli/isli/forecast         
ExecStart=/root/projects/isli/env/bin/gunicorn --log-level debug --error-logfile /var/log/gunicorn/error.log --access-logfile /var/log/gunicorn/access.log --workers 3 --bind unix:/root/projects/isli/isli/forecast/forecast.sock forecast.wsgi:application

[Install]                                                     
WantedBy=multi-user.target

在创建gunicorn.service文件后,我在项目目录中创建了Forecast.sock文件后,运行systemctl start gunicorn比运行systemctl enable gunicorn多了/etc/nginx/sites-available/forecast 比起我在server { listen 165.22.23.233:80; location = /favicon.ico {access_log off; log_not_found off;} location / { include proxy_params; proxy_pass http://unix:/root/projects/isli/isli/forecast/forecast.sock; } } 中用以下

设置nginx
systemctl restart nginx

http://165.22.23.233:80

当我尝试从浏览器访问/var/log/nginx/error.log时,它提示我502错误的网关。在2020/02/09 16:29:01 [crit] 13533#13533: *11 connect() to unix:/root/projects/isli/isli/forecast/forecast.sock failed ( 13: Permission denied) while connecting to upstream, client: 178.176.218.110, server: , request: "GET / HTTP/1.1", upstream: "http://unix:/root/projects/isli/isli/forecast/forecast.sock:/", host: "165.22.23.233" 文件中之后,我看到以下

/root/projects/isli/isli/forecast/forecast.sock

据我了解,通过此错误,我的问题是nginx无法访问namei -nom /root/projects/isli/isli/forecast/forecast.sock 文件。之后,我尝试通过

检查上述路径的每个实体的权限
f: /root/projects/isli/isli/forecast/forecast.sock
drwxr-xr-x root root /
drwx------ root root root
drwxr-xr-x root root projects
drwxr-xr-x root root isli
drwxr-xr-x root root isli
drwxr-xr-x root root forecast
srwxrwxrwx root root forecast.sock

这是输出

def AppendNumbers(input_file, output_file):

    # initialize variables:
    total_number_of_words = 0

    with open(input_file, 'r') as in_file, open(output_file, 'w+') as out_file:

        for line in in_file.readlines():

            # get number of words on each line:
            number_of_words_per_line = len(line.split(' '))

            # add to total word count:
            total_number_of_words += number_of_words_per_line

            # add words to new line:
            new_line = line.replace('\n', '')
            new_line = new_line + ' ' + str(number_of_words_per_line) + ' ' + str(total_number_of_words) + '\n'

            # write new line to outfile:
            out_file.write(new_line)



if __name__ == "__main__":

    input_file = 'file.txt'
    output_file = 'out_file.txt'

    AppendNumbers(input_file, output_file)

在root用户上方的输出中,我的套接字路径具有对每个实体的权限,但为什么错误说权限被拒绝

1 个答案:

答案 0 :(得分:0)

您需要将监听与server_name分开:

server {
        listen 80;
        server_name 165.22.23.233; # (Example 192.168.0.1, example.com)
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
                root /root/projects/isli/isli/forecast/;
        }
        location / {
                include proxy_params;
                proxy_pass http://unix:/root/projects/isli/isli/forecast/forecast.sock;
        }
}