Nginx配置应用

1. 模块说明
1.1 全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。

1.2 events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

1.3 http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

1.4 server块:配置虚拟主机的相关参数,一个http中可以有多个server。

1.5 location块:配置请求的路由,以及各种页面的处理情况。

2. nginx.conf配置

#配置用户或者组,默认为nobody nobody
user root;
#允许生成的进程数,默认为1
worker_processes auto;
#制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
error_log /var/log/nginx/error.log;
#指定nginx进程运行文件存放地址
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
#include /usr/share/nginx/modules/*.conf;

events {
    #最大连接数,默认为512
    worker_connections 1024;
}

http {
    #文件扩展名与文件类型映射表
    include       mime.types;
    #默认文件类型,默认为text/plain
    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  logs/access.log  main;
    access_log  /var/log/nginx/access.log  main;

    #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    #连接超时时间,默认为75s,可以在http,server,location块。
    keepalive_timeout  65;
    #开启gzip
    gzip  on;

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

}

3. 基于nginx.conf配置https(如ngin.conf中的配置路径include)

server {
    listen 80;
    server_name youhost;
    access_log logs/access.log;
    rewrite ^(.*)$  https://$host$1 permanent; 
}

server {
    listen      443 ssl;
    server_name youhost;
    access_log logs/access.log;

    ssl_certificate     /etc/nginx/ssl/youhost/you.pem;
    ssl_certificate_key /etc/nginx/ssl/youhost/you.key;

    ssl_session_timeout  10m;
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;

    location /
    {
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
        proxy_set_header     Host               $host;
        proxy_set_header     X-Real-IP          $remote_addr;
        proxy_set_header     X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header     Cookie             "$http_cookie";
        proxy_hide_header    X-Powered-By;
        proxy_hide_header    X-AspNet-Version;
        proxy_hide_header    X-AspNetMvc-Version;
        proxy_pass           http://127.0.0.1:8088;
    }
 #静态资源、图片等
    location /img/case {
            alias /opt/talent/case/;
            autoindex on;
        }
}
发布了65 篇原创文章 · 获赞 38 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/HelloWorldYangSong/article/details/103763371