Python Nginx 线上环境部署

「爱情、让人受尽委屈。」 2022-05-14 02:55 358阅读 0赞

文章目录

    • Nginx 相关
      • Linux安装Nginx
      • nginx.conf 文件配置
      • 多个 django 虚拟环境 端口配置
    • Django 配置相关
      • 配置 settings.py
      • django项目所有相关静态文件都会收集到制定目录
      • 重启 Nginx

Nginx 相关

Linux安装Nginx

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

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

配置运行的信息

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

nginx.conf 文件配置

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

贴下我们的

  1. user nginx;
  2. worker_processes auto;
  3. worker_cpu_affinity auto;
  4. worker_rlimit_nofile 65535;
  5. events {
  6. use epoll;
  7. multi_accept on;
  8. accept_mutex off;
  9. worker_connections 65535;
  10. }
  11. http {
  12. include /etc/nginx/mime.types;
  13. default_type application/octet-stream;
  14. log_format main '$remote_addr - $remote_user [$time_local] $http_host "$request" '
  15. '$status $body_bytes_sent "$http_referer" '
  16. '"$http_user_agent" "$http_x_forwarded_for" '
  17. '$request_time $upstream_response_time';
  18. log_format jsonold escape=json '{ "@timestamp": "$time_iso8601",'
  19. '"remote_addr": "$remote_addr",'
  20. '"x_forwarded_for": "$http_x_forwarded_for",'
  21. '"time_local": "$time_local",'
  22. '"http_host":"$host",'
  23. '"request": "$request",'
  24. '"request_method": "$request_method",'
  25. '"request_body": "$request_body",'
  26. '"status": "$status",'
  27. '"p_status": "$upstream_status",'
  28. '"body_bytes_sent": "$body_bytes_sent",'
  29. '"http_referer": "$http_referer",'
  30. '"http_user_agent": "$http_user_agent",'
  31. '"request_time": "$request_time",'
  32. '"upstreamhost":"$upstream_addr",'
  33. '"upstream_response_time": "$upstream_response_time" }';
  34. log_format json '{ "@timestamp": "$time_iso8601",'
  35. '"remote_addr": "$remote_addr",'
  36. '"x_forwarded_for": "$http_x_forwarded_for",'
  37. '"time_local": "$time_local",'
  38. '"http_host":"$host",'
  39. '"request": "$request",'
  40. '"request_method": "$request_method",'
  41. '"status": "$status",'
  42. '"p_status": "$upstream_status",'
  43. '"body_bytes_sent": "$body_bytes_sent",'
  44. '"http_referer": "$http_referer",'
  45. '"http_user_agent": "$http_user_agent",'
  46. '"request_time": "$request_time",'
  47. '"upstreamhost":"$upstream_addr",'
  48. '"upstream_response_time": "$upstream_response_time" }';
  49. server_names_hash_bucket_size 4096;
  50. client_header_buffer_size 32k;
  51. large_client_header_buffers 4 32k;
  52. client_max_body_size 50m;
  53. client_body_buffer_size 1m;
  54. client_body_timeout 15;
  55. client_header_timeout 15;
  56. keepalive_timeout 60;
  57. server_tokens off;
  58. sendfile on;
  59. tcp_nopush on;
  60. tcp_nodelay on;
  61. gzip on;
  62. gzip_vary on;
  63. gzip_min_length 1k;
  64. gzip_disable "MSIE [1-6]";
  65. gzip_buffers 16 8k;
  66. gzip_http_version 1.1;
  67. gzip_comp_level 6;
  68. gzip_types text/plain text/css application/xml;
  69. fastcgi_connect_timeout 5s;
  70. fastcgi_send_timeout 6000s;
  71. fastcgi_read_timeout 6000s;
  72. fastcgi_buffer_size 128k;
  73. fastcgi_buffers 256 16k;
  74. fastcgi_busy_buffers_size 1m;
  75. fastcgi_temp_file_write_size 1m;
  76. include /etc/nginx/conf.d/*.conf;
  77. include /etc/nginx/conf.d/test_port/*.conf;
  78. }

多个 django 虚拟环境 端口配置

test_port 目录下 新增文件 test01.conf

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

  1. server {
  2. listen 80;
  3. server_name test.com;
  4. access_log logs/test01.log main;
  5. location /static {
  6. # 一定要注意这一行,工程静态文件的绝对路径
  7. # 不配置的话,关掉django debug 时就会 资源文件 404了
  8. # allstatic 这个目录 请使用下面 django 生成的
  9. alias /data/pythonProject/allstatic;
  10. }
  11. location / {
  12. proxy_connect_timeout 30;
  13. proxy_send_timeout 30;
  14. proxy_read_timeout 30;
  15. proxy_set_header Upgrade $http_upgrade;
  16. proxy_set_header Connection "upgrade";
  17. proxy_http_version 1.1;
  18. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  19. proxy_set_header Host $host;
  20. # 对应的端口 这里我们写 8000
  21. proxy_pass http://localhost:8000;
  22. }
  23. }

Django 配置相关

配置 settings.py

工程名/工程名/settings.py

  1. # 关闭调试模式
  2. # DEBUG = True
  3. # 设置 host
  4. ALLOWED_HOSTS = ['*']
  5. # 数据库配置
  6. # 我这里是相同机房 所以直接使用 localhost
  7. DATABASES = {
  8. 'default': {
  9. 'ENGINE': 'django.db.backends.mysql',
  10. 'NAME': "表名",
  11. 'USER': 'root',
  12. 'PASSWORD': "密码",
  13. 'HOST': "localhost",
  14. 'OPTIONS': { 'init_command': 'SET default_storage_engine=INNODB;' }
  15. }
  16. }
  17. STATIC_URL = '/static/'
  18. STATICFILES_DIRS = (
  19. os.path.join(BASE_DIR, "static"),
  20. )
  21. # 图片路径配置
  22. MEDIA_URL = "/media/"
  23. MEDIA_ROOT = os.path.join(BASE_DIR, "media")
  24. # 正式环境
  25. STATIC_ROOT = os.path.join(BASE_DIR,"allstatic")

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

  1. python manage.py collectstatic

重启 Nginx

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

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

发表评论

表情:
评论列表 (有 0 条评论,358人围观)

还没有评论,来说两句吧...

相关阅读