我的nginx配置
Posted 糖心碎糖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我的nginx配置相关的知识,希望对你有一定的参考价值。
先说一下我的项目的背景,
首先我采用的docker,一台服务器,采用docker-compose编排工具,我计划的是api一个docker(取名-lot-webapi),前端静态页面一个docker(取名:lot-front),后端静态页面一个docker(取名:lot-admin),一个docker的nginx做分流(取名: lot-nginx)
1、首先是分流的docker-nginx
user nginx; #运行用户 worker_processes 1; #启动进程,通常设置成和cpu的数量相等 #全局错误日志及PID文件 #error_log /usr/local/nginx/logs/error.log; #error_log /usr/local/nginx/logs/error.log notice; #error_log /usr/local/nginx/logs/error.log info; #pid /usr/local/nginx/logs/nginx.pid; # 工作模式及连接数上线 events { use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能 worker_connections 1024; #单个后台worker process进程的最大并发链接数 } #设定http服务器,利用它的反向代理功能提供负载均衡支持 http { include mime.types; default_type application/octet-stream; #设定请求缓冲 server_names_hash_bucket_size 128; client_header_buffer_size 32K; large_client_header_buffers 4 32k; # client_max_body_size 8m; #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用, #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfile on; tcp_nopush on; tcp_nodelay on; #连接超时时间 keepalive_timeout 65; #开启gzip压缩,降低传输流量 gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #添加tomcat列表,真实应用服务器都放在这 upstream webapi { #server tomcat地址:端口号 weight表示权值,权值越大,被分配的几率越大; #server 192.168.0.223:8080 weight=4 max_fails=2 fail_timeout=30s; #server 192.168.0.224:8080 weight=4 max_fails=2 fail_timeout=30s; server lot-webapi:8000 fail_timeout=1; } upstream webapp{ server lot-front:80 fail_timeout=1; } upstream webadmin{ server lot-admin:80 fail_timeout=1; } server { listen 80; #监听端口 server_name api.dodododo.club localhost 47.92.33.70; #默认请求设置 location / { proxy_pass http://webapi; #转向.netcore处理 proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Via "nginx"; # add_header ‘Access-Control-Allow-Origin‘ $http_origin; # #add_header ‘Access-Control-Allow-Origin‘ *; # add_header ‘Access-Control-Allow-Credentials‘ ‘true‘; # add_header ‘Access-Control-Allow-Methods‘ ‘GET, POST, OPTIONS‘; # add_header ‘Access-Control-Allow-Headers‘ ‘DNT,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,tkn,platform‘; # add_header ‘Access-Control-Expose-Headers‘ ‘Content-Length,Content-Range‘; # if ($request_method = ‘OPTIONS‘) { # add_header ‘Access-Control-Max-Age‘ 1728000; # add_header ‘Content-Type‘ ‘text/plain; charset=utf-8‘; # add_header ‘Content-Length‘ 0; # return 204; # } } } server { listen 80; server_name lot.dodododo.club; location / { proxy_pass http://webapp; #转向.netcore处理 proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Via "nginx"; } } server { listen 80; server_name admin.dodododo.club; location / { proxy_pass http://webadmin; #转向.netcore处理 proxy_set_header Host $proxy_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Via "nginx"; } } }
简单说一下配置,其他都没什么说的,网上一说一大巴,主要说一下三个upstream吧,三个upstream对应了三个server,每个server我分别绑定了三个域名,根据域名的不同做到分流的目的(其实就分到了三个docker)
关于注释的部分,其实就是跨域的配置,这里其实还绕了不少弯路,因为其实我的webapi里面已经做了跨域的设置,Nginx不应该在做配置。但是我做了,倒是我的前端页面一直请求失败。去掉Nginx的配置(就是注释部分)就行了
前端和后端因为都是静态页面,用Nginx做服务器,所以配置基本差不多,就贴一个就是了
user nginx; #运行用户 worker_processes 1; #启动进程,通常设置成和cpu的数量相等 #全局错误日志及PID文件 #error_log /usr/local/nginx/logs/error.log; #error_log /usr/local/nginx/logs/error.log notice; #error_log /usr/local/nginx/logs/error.log info; #pid /usr/local/nginx/logs/nginx.pid; # 工作模式及连接数上线 events { use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能 worker_connections 1024; #单个后台worker process进程的最大并发链接数 } #设定http服务器,利用它的反向代理功能提供负载均衡支持 http { include mime.types; default_type application/octet-stream; #设定请求缓冲 server_names_hash_bucket_size 128; client_header_buffer_size 32K; large_client_header_buffers 4 32k; # client_max_body_size 8m; #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用, #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfile on; tcp_nopush on; tcp_nodelay on; #连接超时时间 keepalive_timeout 65; #开启gzip压缩,降低传输流量 gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; server { listen 80; #监听端口 server_name admin.dodododo.club; #默认请求设置 location / { root /home/app; index index.html; } } }
docker-compose的配置
version: "2" services: lot-webapi: build: ./docker-app/webapi/ restart: always container_name: lot-webapi volumes: - "./docker-app/webapi/app:/root/app" - "./docker-app/webapi/app/upload:/root/app/upload" - "./docker-app/webapi/app/logs:/root/app/logs" ports: - "9001:8000" # depends_on: # - lot-db links: # - lot-db - lot-redis entrypoint: - dotnet - /root/app/LotteryTicket.Web.dll lot-nginx: build: ./docker-nginx/ container_name: lot-nginx restart: always ports: - "80:80" volumes: - "./docker-nginx/nginx.conf:/etc/nginx/nginx.conf" links: - lot-webapi - lot-front entrypoint: ["nginx", "-g", "daemon off;"] # lot-db: # image: mysql # container_name: lot-db # restart: always # ports: # - "3306:3306" # volumes: # - "./docker-mysql/db-data:/var/lib/mysql" # - "./docker-mysql/my-cnf/my.cnf:/etc/my.cnf" # - "./docker-mysql/init-sql:/docker-entrypoint-initdb.d/" # environment: # - MYSQL_ROOT_PASSWORD=1234qwer # - MYSQL_DATABASE=lotdb lot-rabbitmq: image: rabbitmq:3.7.12-management-alpine container_name: lot-rabbitmq restart: always hostname: lot-rabbitmq ports: - "4369:4369" - "5671:5671" - "5672:5672" - "15671:15671" - "15672:15672" environment: - RABBITMQ_DEFAULT_USER=admin - RABBITMQ_DEFAULT_PASS=1234qweqr lot-redis: build: ./docker-redis/ container_name: lot-redis restart: always ports: - "6636:6636" volumes: - "./docker-redis/redis.conf:/etc/redis/redis.conf" entrypoint: ["redis-server","/etc/redis/redis.conf"] lot-front: build: ./docker-app/webapp/ restart: always container_name: lot-front volumes: - "./docker-app/webapp/nginx.conf:/etc/nginx/nginx.conf" - "./docker-app/webapp/app:/home/app" ports: - "8001:80" lot-admin: build: ./docker-app/webadmin/ restart: always container_name: lot-admin volumes: - "./docker-app/webadmin/nginx.conf:/etc/nginx/nginx.conf" - "./docker-app/webadmin/app:/home/app" ports: - "8002:80"
以上是关于我的nginx配置的主要内容,如果未能解决你的问题,请参考以下文章
Nginx——Nginx启动报错Job for nginx.service failed because the control process exited with error code(代码片段