Python Nginx 线上环境部署

版权声明:本文为博主原创文章,转载请说明出处。 https://blog.csdn.net/qq_30889373/article/details/82284946

Nginx 相关

Linux安装Nginx

因为在公司运维已经安装好了,这里我们就不做讲解了

https://www.cnblogs.com/jimisun/p/8057156.html

配置运行的信息

./nginx -c /etc/nginx/nginx.conf

nginx.conf 文件配置

https://www.cnblogs.com/shamo89/p/7645792.html

贴下我们的

user  nginx;
worker_processes auto;
worker_cpu_affinity auto;
worker_rlimit_nofile 65535;

events {
        use epoll;
        multi_accept on;
        accept_mutex off;
        worker_connections  65535;
}

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

        log_format  main  '$remote_addr - $remote_user [$time_local] $http_host "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for" '
        '$request_time $upstream_response_time';

        log_format jsonold escape=json '{ "@timestamp": "$time_iso8601",'
            '"remote_addr": "$remote_addr",'
            '"x_forwarded_for": "$http_x_forwarded_for",'
            '"time_local": "$time_local",'
            '"http_host":"$host",'
            '"request": "$request",'
            '"request_method": "$request_method",'
            '"request_body": "$request_body",'
            '"status": "$status",'
            '"p_status": "$upstream_status",'
            '"body_bytes_sent": "$body_bytes_sent",'
            '"http_referer": "$http_referer",'
            '"http_user_agent": "$http_user_agent",'
            '"request_time": "$request_time",'
            '"upstreamhost":"$upstream_addr",'
            '"upstream_response_time": "$upstream_response_time" }';

        log_format json '{ "@timestamp": "$time_iso8601",'
            '"remote_addr": "$remote_addr",'
            '"x_forwarded_for": "$http_x_forwarded_for",'
            '"time_local": "$time_local",'
            '"http_host":"$host",'
            '"request": "$request",'
	    '"request_method": "$request_method",'
            '"status": "$status",'
	    '"p_status": "$upstream_status",'
            '"body_bytes_sent": "$body_bytes_sent",'
            '"http_referer": "$http_referer",'
            '"http_user_agent": "$http_user_agent",'
            '"request_time": "$request_time",'
            '"upstreamhost":"$upstream_addr",'
            '"upstream_response_time": "$upstream_response_time" }';

        server_names_hash_bucket_size 4096;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;
        client_body_buffer_size 1m;
        client_body_timeout 15;
        client_header_timeout 15;

        keepalive_timeout  60;
        server_tokens   off;
        sendfile        on;
        tcp_nopush on;
        tcp_nodelay on;

        gzip on;
        gzip_vary on;
        gzip_min_length  1k;
        gzip_disable "MSIE [1-6]";
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_comp_level 6;
        gzip_types       text/plain text/css application/xml;


	fastcgi_connect_timeout 5s;
	fastcgi_send_timeout 6000s;
	fastcgi_read_timeout 6000s;
	fastcgi_buffer_size 128k;
	fastcgi_buffers 256 16k;
	fastcgi_busy_buffers_size 1m;
	fastcgi_temp_file_write_size 1m;

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

多个 django 虚拟环境 端口配置

test_port 目录下 新增文件 test01.conf

这些只是示例
test.com <域名>

server {
        listen 80;
        server_name test.com;
        access_log  logs/test01.log main;
	
	location /static {
		# 一定要注意这一行,工程静态文件的绝对路径
		# 不配置的话,关掉django debug 时就会 资源文件 404了
		# allstatic 这个目录 请使用下面 django 生成的
		alias /data/pythonProject/allstatic;
	}

        location / {
                proxy_connect_timeout 30;
                proxy_send_timeout   30;
                proxy_read_timeout   30;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_http_version 1.1;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                # 对应的端口 这里我们写 8000
                proxy_pass http://localhost:8000;
        }
}

Django 配置相关

配置 settings.py

工程名/工程名/settings.py

# 关闭调试模式
# DEBUG = True

# 设置 host
ALLOWED_HOSTS = ['*']

# 数据库配置
# 我这里是相同机房 所以直接使用 localhost
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': "表名",
        'USER': 'root',
        'PASSWORD': "密码",
        'HOST': "localhost",
        'OPTIONS': { 'init_command': 'SET default_storage_engine=INNODB;' }
    }
}

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
# 图片路径配置
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
# 正式环境
STATIC_ROOT = os.path.join(BASE_DIR,"allstatic")

django项目所有相关静态文件都会收集到制定目录

python manage.py collectstatic

重启 Nginx

https://www.cnblogs.com/fhen/p/5896105.html

扫描二维码关注公众号,回复: 3724709 查看本文章

这个就不说明了,没什么好说的

猜你喜欢

转载自blog.csdn.net/qq_30889373/article/details/82284946