nginx学习:配置文件及其组成

一:配置文件

etc是linux系统放置核心配置的文件夹

/etc/logrotate.d/nginx 配置文件 用于nginx日志轮转,logrotate服务的日志切割

/etc/nginx  目录配置文件  nginx的主要配置文件

/etc/nginx/nginx.conf 主要配置文件  nginx启动会读取的配置文件

/etc/nginx/conf.d 主要配置文件

/etc/nginx/conf.d/default/conf 主要配置文件  默认安装好之后,server加载读取的配置文件

/etc/nginx/nginx.conf

vim nginx,conf

user  nginx;

第一部分:全局块 配置运行nginx服务器组,允许产生worker process数,进程pid存放路径、日志存放路径以及配置文件的引入 worker_processes
1; # nginx服务器并发处理服务的关键配置,值越大,可以支持并发处理量就越多,但是会受到硬件、软件等设备的制约 error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;

第二部分:events块:主要影响nginx服务器与用户的网络连接,常用的设置包括是否开启很多对worker process下的网络连接进行序列化,是否允许同时接受多个网络请求,选取那种事件驱动模型来处理请求,
每个worker process可以同时支持的最大连接数等。1024表示最大连接数位1024个,这部分的配置对nginx的性能影响较大,在实际中应该灵活配置。 events { worker_connections
1024; }

第三部分:http块:nginx配置里面配置最频繁的部分,代理、缓存和日志等绝大多数功能和第三方模块的配置都在这里。http块包括了:http全局块server块 http全局块 http { include
/etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on;

server块
默认的文件里面没有
nginx version: nginx/1.16.1 猜测这个版本里面需要自己配置

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

 server的配置在conf.d里面的default.conf里面,启动后自己默认加载

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
 #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ .php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ .php$ {
 #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /.ht {
    #    deny  all;
    #}
}

二:真实项目中nginx.conf配置

user root;
worker_processes  1;

events {
    worker_connections  1024;
}


http {

    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    #设置允许发布内容为8M
    client_max_body_size 20M;
    client_body_buffer_size 256k;

    upstream django_goat{
        server 127.0.0.1:8008;
    }
upstream django_cow{
        server 127.0.0.1:8009;
    }

    upstream trans{
        server 127.0.0.1:12700;
    }

    upstream cattle{
        server 0.0.0.0:12002;
    }

    upstream breed{
        server 0.0.0.0:12005;
    }

    upstream wxmanage{
        server 0.0.0.0:12003;
    }
upstream usermanage{
        server 0.0.0.0:12006;
    }

    upstream vaccinemanage{
        server 0.0.0.0:12007;
    }

    upstream drinkmanage{
        server 0.0.0.0:12008;
    }

    upstream inoutmanage{
        server 0.0.0.0:12009;
    }

    upstream monitormanage{
        server 0.0.0.0:12011;
    }

    upstream locationmanage{
        server 0.0.0.0:12012;
upstream configmanage{
        server 0.0.0.0:12013;
    }

    upstream bigdata{
        server 0.0.0.0:12015;
    }

    server {
        listen       80;
        listen 443 ssl;
        server_name  www.baoyangny.com;
        ssl_certificate 3367911_www.baoyangny.com.pem;
        ssl_certificate_key 3367911_www.baoyangny.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照这个套件配置
        ssl_prefer_server_ciphers on;

        location / {
            root   html;
            index  index.html index.htm;

 }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /wxmanage {
            proxy_pass http://wxmanage;
        }

    }


    server {

        listen 80;
        listen 443 ssl;
        server_name goat.baoyangny.com;
        ssl_certificate 2513839_goat.baoyangny.com.pem;
        ssl_certificate_key 2513839_goat.baoyangny.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置

ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照这个套件配置
        ssl_prefer_server_ciphers on;


        location / {
            uwsgi_pass  django_goat;
            include     uwsgi_params;
        }

        location /static {
            alias /root/jgw/milk_goat/dist/static;
        }

        location /qldata {
            proxy_pass http://trans/index.html;
        }

        location /qlzy/api/v1/ {
            proxy_pass http://cattle;
        }

        location /qlcattle/api/v1/ {
proxy_pass http://cattle;
        }

        location /api/v1/breed {
            proxy_pass http://breed;
        }

        location /api/v1/user {
            proxy_pass http://usermanage;
        }

        location /api/v1/farm {
            proxy_pass http://usermanage;
        }

        location /api/v1/vaccine {
            proxy_pass http://vaccinemanage;
        }

        location /api/v1/drink {
            proxy_pass http://drinkmanage;
        }
location /api/v1/colony {
            proxy_pass http://inoutmanage;
        }

        location /qlzy/api/v1/monitor {
            proxy_pass http://monitormanage;
        }

        location /api/v1/bdlocation {
            proxy_pass http://locationmanage;
        }

        location /api/v1/big_data {
            proxy_pass http://bigdata;
        }

        location /api/v1/config_manager {
            proxy_pass http://configmanage;
        }

        location /loging {
            if ( $http_host ~* "^(.*)") {

        }     
location /loging {
            if ( $http_host ~* "^(.*)") {
                set $domain $1;
                rewrite ^(.*) http://goat.baoyangny.com/#/loging break;
            }
        }

        location /reging {
            if ( $http_host ~* "^(.*)") {
                set $domain $1;
                rewrite ^(.*) http://goat.baoyangny.com/#/reging break;
            }
        }


    }


    server {
listen 80;
        listen 443 ssl;
        server_name cow.baoyangny.com;
        ssl_certificate 2513843_cow.baoyangny.com.pem;
        ssl_certificate_key 2513843_cow.baoyangny.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照这个套件配置
        ssl_prefer_server_ciphers on;


        location / {
            uwsgi_pass  django_cow;
            #uwsgi_pass  django_goat;
            include     uwsgi_params;
        }


        location /static {
            alias /root/jgw/milk_cow/dist/static;
            #alias /root/jgw/milk_goat/dist/static;
        }
location /qldata {
            proxy_pass http://trans/index.html;
        }

        location /qlcattle/api/v1/ {
            proxy_pass http://cattle;
        }

        location /mycattle/api/v1/ {
            proxy_pass http://cattle;
        }

        location /api/v1/breed {
            proxy_pass http://breed;
        }


        location /api/v1/user {
            proxy_pass http://usermanage;
        }

                                             
        location /api/v1/farm {
            proxy_pass http://usermanage;
        }

        location /api/v1/vaccine {
            proxy_pass http://vaccinemanage;
        }

        location /api/v1/drink {
            proxy_pass http://drinkmanage;
        }

        location /api/v1/colony {
            proxy_pass http://inoutmanage;
        }

        location /qlzy/api/v1/monitor {
            proxy_pass http://monitormanage;

 }

        location /api/v1/bdlocation {
            proxy_pass http://locationmanage;
        }

        location /api/v1/big_data {
            proxy_pass http://bigdata;
        }

        location /api/v1/config_manager {
            proxy_pass http://configmanage;
        }

        location /loging {
            if ( $http_host ~* "^(.*)") {
                set $domain $1;
                rewrite ^(.*) http://cow.baoyangny.com/#/loging break;
            }
        }

        location /reging {

if ( $http_host ~* "^(.*)") {
                set $domain $1;
                rewrite ^(.*) http://cow.baoyangny.com/#/reging break;
            }
        }

        location /qlzy/api/v1/ {
            proxy_pass http://cattle;
        }

    }
                                                                                                                                                                                                      

# TODO

原文地址:https://www.cnblogs.com/meloncodezhang/p/12775137.html