nginx常用服务配置

一、nginx.conf的配置方式,创建新vhost

user nginx;
worker_processes  4;
worker_cpu_affinity 00000001 00000010 00000100 00001000;
worker_rlimit_nofile 204800;

pid        /var/run/nginx.pid;

events {
    worker_connections  204800;
    use epoll;
    multi_accept off;
}

http {
    include       /usr/local/nginx/conf/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"‘;
    log_format  mtr  ‘$remote_addr [$time_local] "$request_uri" ‘
                      ‘$status "$http_referer" ‘
                      ‘"$http_user_agent" "$host"‘;

    sendfile        on;

    keepalive_timeout  30;
    client_header_timeout    30;
    client_body_timeout    40;
    server_tokens off;
    tcp_nodelay        on;

    gzip  on;

    include /usr/local/nginx/conf/vhost/*.conf;

    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    #fastcgi_buffer_size 16k;
    #fastcgi_buffers 16 16k;
    #fastcgi_busy_buffers_size 16k;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 2k;
    large_client_header_buffers 4 4k;
    client_max_body_size 100k;
    open_file_cache max=51200 inactive=20s;
    open_file_cache_valid   30s;
    open_file_cache_min_uses 1;
}

nginx.conf

之后新的服务写入vhost文件夹

二、配置nginx需要的服务

1)对html服务的配置(只包含html,js,png文件)

server {
        listen       800;
        server_name  127.0.0.1;
        #server_name sch5.com.cn;

        access_log  /var/log/nginx/sch5.com.cn_access.log  main;
    error_log  /var/log/nginx/sch5.talkweb.com.cn_error.log;

    proxy_set_header                    Host $host;
    proxy_set_header                    X-Forwarded-For $remote_addr;
    proxy_set_header                    X-Real_IP $remote_addr;

    disable_symlinks off;
        location  / {
        root   /opt/wwwroot/;
            index  index.php index.html index.htm;
        }

        location /nginx_status {
            #stub_status on;
            allow 127.0.0.1;
            deny all;
        }
}

html.conf

2) 对django项目的配置

server {
    listen       888;
    server_name  localhost;

        access_log  /data/log/nginx/myjumpserver_access.log  main;
        error_log  /data/log/nginx/myjumpserver_error.log;

    location / {
            uwsgi_pass   192.168.10.55:8888;
            include uwsgi_params;
    }
        # django项目文件, MyJumpserver,静态资源这里加载
        location /static {
            alias /opt/wwwroot/MyJumpserver/static/;
        }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
# 访问localhost:80 ===>192.168.10.13:8888(uwsgi服务提供的)

3)对django项目后台管理的配置

server {
    listen       8000;
    server_name  localhost;

        access_log  /data/log/nginx/pvzstar_access.log  main;
        error_log  /data/log/nginx/pvzstar_error.log;

    location / {
            uwsgi_pass   192.168.2.155:8888;
            include uwsgi_params;
    }
        # django项目文件, MyJumpserver,静态资源这里加载
        location /static {
            alias /usr/local/python3/lib/python3.6/site-packages/django/contrib/admin/static/;
        }

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

django_admin.conf

4)配置php项目

server {
        listen       80;
        server_name  test.php.com;
        #server_name  192.168.2.41;

        access_log  /data/log/nginx/test.php_access.log  main;

    error_log  /data/log/nginx/test.php_error.log;

    proxy_set_header                    Host $host;
    proxy_set_header                    X-Forwarded-For $remote_addr;
    proxy_set_header                    X-Real_IP $remote_addr;

        location  / {
        root   /opt/wwwroot/test.php.com.cn/;
            index  index.php index.html index.htm;
        }

        location /nginx_status {
            stub_status on;
            allow 127.0.0.1;
            deny all;
        }
        error_page  404              /404.html;
        location = /404.html {
            root   /usr/share/nginx/html;
        }

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

        location ~ .*\.php$ {
            fastcgi_pass   127.0.0.1:9000;
            #fastcgi_pass   UNIX:/tmp/php-cgi.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /opt/wwwroot/test.php.com.cn$fastcgi_script_name;
            include        fastcgi_params;
          if ( $fastcgi_script_name ~ \..*\/.*php ) {
                return 403;
            }
        }
}

php.conf

5) nginx配置zabbix服务

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /opt/lnmp_zabbix/nginx/conf/mime.types;
    default_type  application/octet-stream;

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       89;
        server_name  localhost;
        #access_log /opt/lnmp_zabbix/nginx/log/zabbix.log main;
        index index.html index.php index.html;
        root /opt/wwwroot/zabbix;        

        location / {
            try_files $uri $uri/ /index.php?$args;
        }

      location ~ ^(.+.php)(.*)$ {
        fastcgi_split_path_info ^(.+.php)(.*)$;
        include fastcgi.conf;
        fastcgi_pass 127.0.0.1:9001;
        fastcgi_index index.php;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

6) 查看nginx的连接状态

    server {
        listen 8080 default;
        location /nginx_status {
            stub_status on;
            allow 127.0.0.1;
            deny all;
        }
        location /{
            return 403;
        }
    }

三、nginx作为代理服务

1)代理tcp协议

user nginx;
worker_processes  4;
worker_cpu_affinity 00000001 00000010 00000100 00001000;
worker_rlimit_nofile 204800;

pid        /var/run/nginx.pid;

events {
    worker_connections  204800;
    use epoll;
    multi_accept off;
}

http {
    include       /opt/lnmp_zabbix/nginx/conf/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"‘;
    log_format  mtr  ‘$remote_addr [$time_local] "$request_uri" ‘
                      ‘$status "$http_referer" ‘
                      ‘"$http_user_agent" "$host"‘;

    sendfile        on;

    keepalive_timeout  30;
    client_header_timeout    30;
    client_body_timeout    40;
    server_tokens off;
    tcp_nodelay        on;

    gzip  on;

    include /opt/lnmp_zabbix/nginx/conf/vhost/*.conf;

    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    #fastcgi_buffer_size 16k;
    #fastcgi_buffers 16 16k;
    #fastcgi_busy_buffers_size 16k;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 2k;
    large_client_header_buffers 4 4k;
    client_max_body_size 100k;
    open_file_cache max=51200 inactive=20s;
    open_file_cache_valid   30s;
    open_file_cache_min_uses 1;
}

stream {

    upstream cloudsocket {
       hash $remote_addr consistent;
      # $binary_remote_addr;
       server 192.168.0.12:3306 weight=5 max_fails=3 fail_timeout=30s;
    }
    server {
       listen 80;#数据库服务器监听端口
       proxy_connect_timeout 10s;
       proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
       proxy_pass cloudsocket;
    }
}

stream.conf

2.1)代理http服务

server {
    listen      10051;
    server_name  110.110.110.110;
    charset utf8;

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

2.2) 代理http的更多参数优化

upstream 192.168.1.29 {
      server  192.168.1.25:80;
}

server {
    listen      80;
    server_name  192.168.1.29;
    charset utf8;

    location / {
        proxy_pass        http://192.168.1.29;
        proxy_set_header  Host            $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
}

2.3) 代理http服务, 加入白名单

server {
    listen      80;
    server_name  192.168.2.95;
    access_log /opt/lnmp_zabbix/nginx/log/zabbix2.log main;
    error_log /opt/lnmp_zabbix/nginx/log/zabbix_error2.log crit;
    charset utf8;

    location / {
        allow             192.168.2.5;  # 代理服务下,只有这个ip可以去访问 192.168.2.95:80
        proxy_pass        http://192.168.2.90:89;
        deny              all;
    }

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

http.conf

3) 实现nginx的负载均衡

upstream 192.168.1.29 {
      server  192.168.1.25:80;
      server  192.168.1.26:80;
}

server {
    listen      80;
    server_name  192.168.1.29;
    charset utf8;

    location / {
        proxy_pass        http://192.168.1.29;
        proxy_set_header  Host            $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    }
}

4)网络代理。现象:有2台互通的机器,但是有一台有网络,有一台,没有网络,因为没有dns,如何让没有网络的机器能使用yum源

server {
    resolver 192.168.10.1 192.168.2.1;    # dns
    resolver_timeout 5s;

    listen 8000;
    server_name 0.0.0.0;
    access_log /data/log/nginx/myjumpserver_access.log main;
    error_log /data/log/nginx/myjumpserver_error.log;
    location / {
                        proxy_pass $scheme://$host$request_uri;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $Host;
            proxy_set_header X-Forwarder-For $Host;
            proxy_buffering on;
            proxy_max_temp_file_size 0;
            proxy_cache_valid 200 320 10m;
            proxy_cache_valid 301 1h;
            proxy_cache_valid any 1m;
            # include /data/app/nginx/conf/proxy.conf;
    }
}

network.conf

原文地址:https://www.cnblogs.com/linu/p/10430197.html

时间: 2024-10-11 02:10:29

nginx常用服务配置的相关文章

Kali Linux常用服务配置教程安装及配置DHCP服务

Kali Linux常用服务配置教程安装及配置DHCP服务 在Kali Linux中,默认没有安装DHCP服务.下面将介绍安装并配置DHCP服务的方法. 1.安装DHCP服务 在Kali Linux中,用来提供DHCP服务的安装包名为isc-dhcp-server.所以,执行命令如下所示: [email protected]:~# apt-get install isc-dhcp-server –y 执行以上命令后,将开始安装DHCP服务.如果安装过程中没有报错的话,则该服务将被成功安装到系统中

centos7服务搭建常用服务配置之一:SSH

目录 1 SSH服务协议 1.1 ssh服务协议说明 1.2 ssh服务工作机制 1.3 ssh加密技术说明 1.3.1 ssh实现安全链接建立,利用要是和锁头 1.3.2 ssh加密算法 1.4 ssh知识要点 2.ssh服务软件详细说明 2.1 ssh服务软件安装 2.2 openssh-clients软件的主要内容 2.3 openssh-server软件的主要内容 2.4 启动sshd服务 2.5 ssh服务配置文件说明 2.5.1 ssh服务的配置文件路径 2.5.3 配置文件语法检查

centos7服务搭建常用服务配置之二:Rsync+sersync实现数据实时同步

目录 1.RSYNC数据备份 1.1 rsync服务简介 1.2 rsync特点和优势 1.3 rysnc运行模式简介 1.4 数据同步方式 2 Rsync实验测试 2.1 实验环境说明 2.2 服务安装 2.3 Rsync命令详解 2.3 实验一使用rsync命令备份数据 2.4 实验二使用rsyncd服务进行数据备份 3.Rsync+sersync实现数据实时同步 3.1 rsync+sersync架构作用 3.2 rsync+inotify-tools与rsync+sersync架构的区别

Nginx常用功能配置及优化

---------------------------------------------------------------------------------------- 规范优化Nginx配置文件: ---------------------------------------------------------------------------------------- Nginx的主配置文件为nginx.conf,主配置文件包含所有虚拟主机的子配置文件同一放到extra目录中. 虚

nginx常用功能配置

一.规范优化nginx配置文件 nginx的主配置文件为nginx.conf,主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目录中,虚拟主机的配置文件按照网站的域名或功能取名,例如www.conf.bbs.conf.blog.conf等.当然,如果虚拟主机的数量不是很多,也可以把多个虚拟主机配置成一个单独的配置文件,仅仅和nginx的主配置文件 nginx.conf分离开即可. 这里使用的参数是include,下面先看看它的语法: include file | mask 它可以放置

Nginx常用配置实例(4)

Nginx作为一个HTTP服务器,在功能实现方面和性能方面都表现得非常卓越,完全可以与Apache相媲美,几乎可以实现Apache的所有功能,下面就介绍一些Nginx常用的配置实例,具体包含虚拟主机配置.负载均衡配置.防盗链配置以及日志管理等. 一. 虚拟主机配置实例 下面在Nginx中创建三个虚拟主机,需要说明的是,这里仅仅列出了虚拟主机配置部分. http { server { listen          80; server_name     www.domain1.com; acce

Nginx 网站服务——虚拟主机配置

第1章 Nginx 网站服务 1.1 web网站服务介绍: 1.1.1 提供静态服务的软件 Apache:这是中小型Web服务的主流,Web服务器中的老大哥. Nginx:大型网站Web服务的主流,曾经Web服务器中的初生牛犊,现已长大. Nginx的分支Tengine(http://tengine.taobao.org/)目前也在飞速发展. Lighttpd:这是一个不温不火的优秀Web软件,社区不活跃,静态解析效率很高.在Nginx流行前,它是大并发静态业务的首选,国内百度贴吧.豆瓣等众多网

搭建高性能web服务器之Nginx安装与配置(2.3)

<上一章节介绍了如何获取Nginx以及如何配置.编译.安装运行Nginx.但是很多情况下我们是根据需要来编译Nginx,这里不得不说道nginx的./configure相关参数> 一 Nginx的./configure编译参数说明介绍 可以看出,configure命令至关重要,比如根据自己需要选择性的安装nginx是很有必要的,下文将详细介绍如何使用configure命令使用方法. 我们在解压了nginx的源码后,进入到nginx的源码目录使用"./configure --help&

nginx常用配置系列-反向代理

接上篇,反向代理的原理与用途很多地方有讲,用文字说再多可能也表达不清楚,下面贴一张拓扑图,介绍下什么叫反向代理 以上图有两种情景 1. 访问者的客户端是 local ,要访问baidu的服务器,baidu的前台服务器本身不处理具体的业务,只是根据访问的数据类型,或者业务类型等(就是一些特定的规则,比如URL正则),将不同类的请求转发到不同的后端服务器,例如server1是静态资源的,server2是处理账户系统的等 2. 后端的每个server提供的服务完全相同,baidu的前台服务器根据后端每