常见的 nginx 配置文件的写法

olei 3,476 views 0

http与https按照给php环境配置的样子举例

http

server {
  listen 80;
  root /xxx/xxx/xxx;  # 项目目录
  index index.html index.php;
  server_name domain.xx.xx; # 绑定的域名或者ip
  server_tokens off;

    location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
        expires max;
        add_header Cache-Control public;
        access_log off;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        autoindex on;

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $realpath_root/$fastcgi_script_name;
        }
    }

    location ~ /\.ht {
        deny all;
    }
}

https

server {
  listen 80;
  root xx/xx/xx/xx; #项目目录
  index index.html index.php;
  server_name domain.xx.xx; # 绑定的域名
  server_tokens off;
  return 301 https://domain.xx.xx$request_uri; # 强制跳转https
}

server {
    listen 443 ssl http2;
    root xx/xx/xx/xx; #项目目录
    index index.html index.php;
    server_name domain.xx.xx; # 绑定的域名
    client_max_body_size 0;
    server_tokens off;
    ssl_certificate /xx/xxx/xxx/fullchain.pem; # 证书内容路径
    ssl_certificate_key /xxx/xxx/xxx/privkey.key; # 证书key路径
    ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4';
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_session_timeout  5m;

    location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
        expires max;
        add_header Cache-Control public;
        access_log off;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $realpath_root/$fastcgi_script_name;
        }
    }

    location ~ /\.ht {
        deny all;
    }
}

https的配置文件加入了强制301跳转到https上,以上的证书需要申请,快速申请详见:详情

proxy+https

server {
    listen 443 ssl http2;
    server_name domain.xx.xx; #绑定的域名
    ssl_certificate /xxx/xxx/xxx/fullchain.pem; # 证书路径
    ssl_certificate_key /xxx/xxx/xxx/privkey.key; # key路径
    ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4';
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_session_timeout  5m;

    proxy_set_header   Host                 $http_host;
    proxy_set_header   X-Forwarded-Proto    $scheme;
    proxy_set_header   X-Forwarded-For      $remote_addr;
    proxy_redirect     off;

    # keepalive + raven.js is a disaster
    keepalive_timeout 0;

    # use very aggressive timeouts
    proxy_read_timeout 5s;
    proxy_send_timeout 5s;
    send_timeout 5s;
    resolver_timeout 5s;
    client_body_timeout 5s;

    # buffer larger messages
    client_max_body_size 5m;
    client_body_buffer_size 100k;
    location / {
        add_header Strict-Transport-Security "max-age=31536000";

        proxy_pass http://127.0.0.1:9001/; #nginx代理运行程序所在的端口
    }
}

proxy+nginx适用于运行代理比如tomcat的8080端口的程序等等

uwsgi+nginx(https)

server {
  listen 80;
  index index.html index.php;
  server_name domain.xxx.xx; # 绑定的域名
  server_tokens off;
  return 301 https://domain.xxx.xx$request_uri; # 强制跳转https
}

server {
    listen 443 ssl http2;
    server_name domain.xxx.xx; # 绑定的域名
    ssl_certificate /xx/xxx/xx.crt; # 证书路径
    ssl_certificate_key /xxx/xxx/xxx.key; # 证书key路径
    ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4';
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_session_timeout  5m;

    client_max_body_size 75M;

    # uwsgi相关
    location / {
       include uwsgi_params;
       uwsgi_pass 127.0.0.1:8765; # 自定义的uwsgi文件端口
       uwsgi_read_timeout 2;
    }

    # 静态文件位置相关
    location /static/ {
        expires 30d;
        autoindex on;
        add_header Cache-Control private;
        alias /xxx/xxx/xxx/;  # 静态文件位置
     }
}

uwsgi+nginx适用于比如python的flask/django的情况,之前需要配置一下uwsgi文件

发表评论 取消回复
表情 图片 链接 代码

分享