优秀的编程知识分享平台

网站首页 > 技术文章 正文

Nginx配置详解FTP搭建静态文件https服务器

nanyue 2025-01-21 20:23:58 技术文章 6 ℃

目录

  • Nginx配置详解
  • 序言
  • Nginx常用功能
  • Nginx配置文件结构
  • nginx+ftp搭建图片服务器(Windows Server服务器环境下)
  • windows下用nginx配置https服务器
  • Nginx httpS server配置


Nginx配置详解

回到目录

序言

Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的。从2004年发布至今,凭借开源的力量,已经接近成熟与完善。

Nginx功能丰富,可作为HTTP服务器,也可作为反向代理服务器,邮件服务器。支持FastCGI、SSL、Virtual Host、URL Rewrite、Gzip等功能。并且支持很多第三方的模块扩展。

Nginx的稳定性、功能集、示例配置文件和低系统资源的消耗让他后来居上,在全球活跃的网站中有12.18%的使用比率,大约为2220万个网站。

牛逼吹的差不多啦,如果你还不过瘾,你可以百度百科或者一些书上找到这样的夸耀,比比皆是。

回到目录

Nginx常用功能

1、Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理。

这里我给来2张图,对正向代理与反响代理做个诠释,具体细节,大家可以翻阅下资料。

Nginx在做反向代理时,提供性能稳定,并且能够提供配置灵活的转发功能。Nginx可以根据不同的正则匹配,采取不同的转发策略,比如图片文件结尾的走文件服务器,动态页面走web服务器,只要你正则写的没问题,又有相对应的服务器解决方案,你就可以随心所欲的玩。并且Nginx对返回结果进行错误页跳转,异常判断等。如果被分发的服务器存在异常,他可以将请求重新转发给另外一台服务器,然后自动去除异常服务器。

2、负载均衡

Nginx提供的负载均衡策略有2种:内置策略和扩展策略。内置策略为轮询,加权轮询,Ip hash。扩展策略,就天马行空,只有你想不到的没有他做不到的啦,你可以参照所有的负载均衡算法,给他一一找出来做下实现。

上3个图,理解这三种负载均衡算法的实现

Ip hash算法,对客户端请求的ip进行hash操作,然后根据hash结果将同一个客户端ip的请求分发给同一台服务器进行处理,可以解决session不共享的问题。

3、web缓存

Nginx可以对不同的文件做不同的缓存处理,配置灵活,并且支持FastCGI_Cache,主要用于对FastCGI的动态程序进行缓存。配合着第三方的ngx_cache_purge,对制定的URL缓存内容可以的进行增删管理。

4、Nginx相关地址

源码:https://trac.nginx.org/nginx/browser

官网:http://www.nginx.org/

回到目录

Nginx配置文件结构

如果你下载好啦,你的安装文件,不妨打开conf文件夹的nginx.conf文件,Nginx服务器的基础配置,默认的配置也存放在此。

在nginx.conf的注释符号位#

nginx文件的结构,这个对刚入门的同学,可以多看两眼。

默认的config

nginx文件结构

...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}

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

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

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

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

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

下面给大家上一个配置文件,作为理解,同时也配入我搭建的一台测试机中,给大家示例。

########### 每个指令必须有分号结束。#################
#user administrator administrators;  #配置用户或者组,默认为nobody nobody。
#worker_processes 2;  #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址
error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on
    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
    #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大连接数,默认为512
}
http {
    include       mime.types;   #文件扩展名与文件类型映射表
    default_type  application/octet-stream; #默认文件类型,默认为text/plain
    #access_log off; #取消服务日志    
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
    access_log log/access.log myFormat;  #combined为日志格式的默认值
    sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
    keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。

    upstream mysvr {   
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #热备
    }
    error_page 404 https://www.baidu.com; #错误页
    server {
        keepalive_requests 120; #单连接请求上限次数。
        listen       4545;   #监听端口
        server_name  127.0.0.1;   #监听地址       
        location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
           #root path;  #根目录
           #index vv.txt;  #设置默认页
           proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
           deny 127.0.0.1;  #拒绝的ip
           allow 172.18.5.54; #允许的ip           
        } 
    }
} 

上面是nginx的基本配置,需要注意的有以下几点:

1、1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址; 2.$remote_user :用来记录客户端用户名称; 3.$time_local : 用来记录访问时间与时区;4.$request : 用来记录请求的url与http协议;

5.$status : 用来记录请求状态;成功是200, 6.$body_bytes_s ent :记录发送给客户端文件主体内容大小;7.$http_referer :用来记录从那个页面链接访问过来的; 8.$http_user_agent :记录客户端浏览器的相关信息;

2、惊群现象:一个网路连接到来,多个睡眠的进程被同事叫醒,但只有一个进程能获得链接,这样会影响系统性能。

3、每个指令必须有分号结束。

回到目录

nginx+ftp搭建图片服务器(Windows Server服务器环境下)

几种图片服务器的对比

1、直接使用ftp服务器,访问图片路径为 ftp://账户:密码@192.168.0.106/31275-105.jpg

不采用这种方式,不安全容易暴露ftp账户信息

2、直接使用IIS或Tomcat等服务器在项目中访问,图片少的情况可以考虑。商城网站则不行,图片访问处理需搭建图片服务器

3、ftp+nginx服务器,ftp负责上传图片,nginx负责图片的访问

一、需要的组件

1、ftp服务器(图片上传,本文选用IIS为容器)

ps Linux操作系统 可安装vsftpd作为服务器

2、nginx服务器

a、http服务:可以使用nginx做静态资源服务器。也可以使用apache。推荐使用nginx,效率更高。

b、反向代理 实现 负载均衡

二、nginx服务器的部署

nginx下载地址 http://nginx.org/en/download.html

解压安装包,免安装运行

nginx cmd命令

start nginx //启动nginx

nginx -s stop // 停止nginx

nginx -s reload // 重新加载配置文件

nginx -s quit // 退出nginx

nginx -t //检查配置文件是否正确

nginx -v //查看nginx版本号

1、配置端口号和访问路径

conf目录下的 nginx.conf 配置文件

配置文件介绍戳这 http://www.cnblogs.com/knowledgesea/p/5175711.html

修改端口号 为9090

启动nginx 回到nginx文件夹根目录,按住shift键点击鼠标右键,选择右键菜单中的在此处打开命令窗口,输入start nginx 命令

成功访问

2、添加图片服务访问配置

1234location ~ \.(gif|jpg|jpeg|png|bmp|swf)$ {            #默认的图片路径,也是ftp上传文件存放的路径,只要后缀是以上的都会到这个路径下搜索        root         C:/imgextra;    }

添加完成以后在cmd中执行 nginx -s reload 重新加载配置使其生效

图片文件所在路径


访问成功

3、图片存放路径分析

先来看看大型商城的图片路径

天猫图片存储

https://gdp.alicdn.com/imgextra/i2/1856815898/TB2QxuMuHBnpuFjSZFGXXX51pXa_!!1856815898.jpg

京东图片存储

https://img30.360buyimg.com/sku/jfs/t4816/236/2599170601/86961/b24bbc4/5902ff58Nc4ceea7c.jpg

当当图片存储

http://img3x5.ddimg.cn/19/20/1206933175-1_x_6.jpg

分析发现几乎都采用CDN和单独的域名来作为独立的图片服务器,目的是为了减少业务服务器的并发访问量。

关于图片服务器架构介绍戳这里

http://blog.csdn.net/dinglang_2009/article/details/31450731

三、FTP服务的安装与使用介绍

1、windows中安装使用FTP服务器请参考

http://blog.csdn.net/w1014074794/article/details/52075285

2、java上传文件到FTP服务器工具类

所需jar包 commons-net-1.4.1.jar

https://files.cnblogs.com/files/cczheng-666/commons-net-1.4.1.zip

回到目录

windows下用nginx配置https服务器

1.安装nginx

  • 先到nginx官网下在nginx http://nginx.org/en/download.html
  • 将下载好的文件解压出来修改文件名为 nginx ,然后拷贝到C盘下,目录如下:
  • 运行 nginx
start nginx
  • 验证
在浏览器中输入 localhost 访问即可,如出现以下页面,即安装成功

2.安装 OpenSSL

  • 下载OpenSSL http://slproweb.com/products/Win32OpenSSL.html
  • 下载完成安装到 C:\OpenSSL-Win64
  • 配置环境变量

在path变量后需要加入 %OPENSSL_HOME%

3.生成https证书

  • 在C:\nginx下创建ssl文件夹 用于存放证书
  • 创建私钥 (建议使用系统窗口,不要用gitBash 有涉及到选择的地方,gitBash无法选择)
openssl genrsa -des3 -out shidian.key 1024 //shidian 自己取的名字

效果如下:

  • 创建 csr 证书
openssl req -new -key shidian.key -out shidian.csr
xxxxxxxxxxbr openssl req -new -key shidian.key -out shidian.csrbr

此时效果:

  • 删除密码
    复制 shidian.key 并重命名 shidian.key.org

openssl rsa -in shidian.key.org -out shidian.key

  • 生成crt证书

openssl x509 -req -days 365 -in shidian.csr -signkey shidian.key -out shidian.crt

  • 最后生成证书如下

修改 nginx 下的 nginx.conf配置文件

C:\nginx\conf\nginx.conf
xxxxxxxxxxbr C:\nginx\conf\nginx.confbr
upstream nodejs__upstream2 {
	server 127.0.0.1:8080; # 需要监听的端口名 我用的
	keepalive 64;
}

server {
	listen 443 ssl;
	server_name dev.kt.looklook.cn; # 配置的https的域名

	ssl_certificate      C://nginx//ssl//shidian.crt;  # 这个是证书的crt文件所在目录
	ssl_certificate_key  C://nginx//ssl//shidian.key;  # 这个是证书key文件所在目录

	ssl_session_cache    shared:SSL:1m;
	ssl_session_timeout  5m;

	ssl_ciphers  HIGH:!aNULL:!MD5;
	ssl_prefer_server_ciphers  on;

 location / {
	proxy_set_header   X-Real-IP            $remote_addr;
	proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
	proxy_set_header   Host                   $http_host;
	proxy_set_header   X-NginX-Proxy    true;
	proxy_set_header   Connection "";
	proxy_http_version 1.1;
	proxy_pass         http://nodejs__upstream2;
 }
}
xxxxxxxxxxbr upstream nodejs__upstream2 {br  server 127.0.0.1:8080; # 需要监听的端口名 我用的br  keepalive 64;br}brbrserver {br  listen 443 ssl;br  server_name dev.kt.looklook.cn; # 配置的https的域名brbr  ssl_certificate      C://nginx//ssl//shidian.crt;  # 这个是证书的crt文件所在目录br  ssl_certificate_key  C://nginx//ssl//shidian.key;  # 这个是证书key文件所在目录brbr  ssl_session_cache    shared:SSL:1m;br  ssl_session_timeout  5m;brbr  ssl_ciphers  HIGH:!aNULL:!MD5;br  ssl_prefer_server_ciphers  on;brbr location / {br  proxy_set_header   X-Real-IP            $remote_addr;br  proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;br  proxy_set_header   Host                   $http_host;br  proxy_set_header   X-NginX-Proxy    true;br  proxy_set_header   Connection "";br  proxy_http_version 1.1;br  proxy_pass         http://nodejs__upstream2;br }br}br
  • 重启nginx
nginx -s reload
xxxxxxxxxxbr nginx -s reloadbr
  • 配置host文件
C:\Windows\System32\drivers\etc 路径下
127.0.0.1          dev.kt.looklook.cn  #需要配置的域名
xxxxxxxxxxbr C:\Windows\System32\drivers\etc 路径下br127.0.0.1          dev.kt.looklook.cn  #需要配置的域名br
  • 访问
输入你配置好的域名即可访问了
xxxxxxxxxxbr 输入你配置好的域名即可访问了br

奋斗就是每天很难,可是一年一年却越来越容易; 不奋斗就是每天很容易,可是一年一年却越来越难。

回到目录

Nginx httpS server配置

Nginx httpS 配置

配置同时支持http和httpS协议:
xxxxxxxxxxbr 配置同时支持http和httpS协议:

server {
        listen 80 default backlog=2048;
     #backlog:每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目。
        listen 443 ssl;
        server_name ssl.joy4you.com;
        ssl_certificate  /data/nginx/conf/server.crt;
        ssl_certificate_key  /data/nginx/conf/server_nopwd.key;

        root /data/;
        location / {
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$args;
}
         location ~ .*\.(php|php5)?$ {
#            try_files $uri =404;
             fastcgi_pass    127.0.0.1:9000;
             fastcgi_index   index.php;
#            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
             include         fastcgi.conf;
}
        }
xxxxxxxxxxbr brserver {br        listen 80 default backlog=2048;br     #backlog:每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许送到队列的数据包的最大数目。br        listen 443 ssl;br        server_name ssl.joy4you.com;br        ssl_certificate  /data/nginx/conf/server.crt;br        ssl_certificate_key  /data/nginx/conf/server_nopwd.key;brbr        root /data/;br        location / {br        index index.php index.html index.htm;br        try_files $uri $uri/ /index.php?$args;br}br         location ~ .*\.(php|php5)?$ {br#            try_files $uri =404;br             fastcgi_pass    127.0.0.1:9000;br             fastcgi_index   index.php;br#            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;br             include         fastcgi.conf;br}br        }
配置/data/http/使用http协议;/data/ssl/使用httpS协议:
xxxxxxxxxxbr 配置/data/http/使用http协议;/data/ssl/使用httpS协议:

server {
        listen       80;
        server_name  192.168.17.16;
        access_log  /data/nginx/logs/php.joy4you.com.log  main;
        root /data/http/;

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

         location ~ .*\.(php|php5)?$ {
#            try_files $uri =404;
             fastcgi_pass    127.0.0.1:9000;
             fastcgi_index   index.php;
#            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
             include         fastcgi.conf;
}
}

server {
        listen       443;
        ssl     on;
        ssl_certificate  /data/nginx/conf/server.crt;
        ssl_certificate_key  /data/nginx/conf/server_nopwd.key;
        server_name  192.168.17.16;
        access_log  /data/nginx/logs/php.joy4you.com.log  main;
        root /data/ssl/;

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

         location ~ .*\.(php|php5)?$ {
#            try_files $uri =404;
             fastcgi_pass    127.0.0.1:9000;
             fastcgi_index   index.php;
#            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
             include         fastcgi.conf;
}
}
 brserver {br        listen       80;br        server_name  192.168.17.16;br        access_log  /data/nginx/logs/php.joy4you.com.log  main;br        root /data/http/;brbr        location / {br        index index.php index.html index.htm;br        try_files $uri $uri/ /index.php?$args;br}brbr         location ~ .*\.(php|php5)?$ {br#            try_files $uri =404;br             fastcgi_pass    127.0.0.1:9000;br             fastcgi_index   index.php;br#            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;br             include         fastcgi.conf;br}br}brbrserver {br        listen       443;br        ssl     on;br        ssl_certificate  /data/nginx/conf/server.crt;br        ssl_certificate_key  /data/nginx/conf/server_nopwd.key;br        server_name  192.168.17.16;br        access_log  /data/nginx/logs/php.joy4you.com.log  main;br        root /data/ssl/;brbr        location / {br        index index.php index.html index.htm;br        try_files $uri $uri/ /index.php?$args;br}brbr         location ~ .*\.(php|php5)?$ {br#            try_files $uri =404;br             fastcgi_pass    127.0.0.1:9000;br             fastcgi_index   index.php;br#            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;br             include         fastcgi.conf;br}br}
把访问80端口的请求全部转发到443(https):
xxxxxxxxxxbr 把访问80端口的请求全部转发到443(https):

server {
        listen       80;
        server_name  192.168.17.16;
        rewrite ^(.*) https://$server_name$1 permanent;
}

server {
        listen       443;
        ssl     on;
        ssl_certificate  /data/nginx/conf/server.crt;
        ssl_certificate_key  /data/nginx/conf/server_nopwd.key;
        server_name  192.168.17.16;
        access_log  /data/nginx/logs/php.joy4you.com.log  main;
        root /data/;

        location / {
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$args;
}
         location ~ .*\.(php|php5)?$ {
#            try_files $uri =404;
             fastcgi_pass    127.0.0.1:9000;
             fastcgi_index   index.php;
#            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
             include         fastcgi.conf;
}
}
#            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

使用沃通的CA证书,他们推荐的https配置:

server {
 listen      443;
 server_name  localhost;
 
 #为一个server开启ssl支持
 ssl                  on;
 
 #为虚拟主机指定pem格式的证书文件
 ssl_certificate      /home/wangzhengyi/ssl/wangzhengyi.crt;
 
 #为虚拟主机指定私钥文件
 ssl_certificate_key  /home/wangzhengyi/ssl/wangzhengyi_nopass.key;
 
 #客户端能够重复使用存储在缓存中的会话参数时间
 ssl_session_timeout  5m;
 
 #指定使用的ssl协议
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 
 #指定许可的密码描述
 ssl_ciphers  ALL:!ADH:!EXPORT56: -RC4+RSA:+HIGH:+MEDIUM: !EXP;
#ssl_ciphers  ALL:!ADH:!EXPORT56: -RC4+RSA:+HIGH:+MEDIUM:-EXP;
 
 #SSLv3和TLSv1协议的服务器密码需求优先级高于客户端密码
 ssl_prefer_server_ciphers  on;
 ssl_certificate_key  /home/wangzhengyi/ssl/wangzhengyi_nopass.key;

SLL参数:

ssl_session_timeout 5m;   ##设置客户端能够反复使用储存在缓存中的会话参数时间。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  ##指定要开启的SSL协议。
ssl_ciphers ALL:!ADH:!EXPORT56:-RC4+RSA:+HIGH:+MEDIUM:!EXP; ##指出为建立安全连接,服务器所允许的密码格式列表,密码指定为OpenSSL支持的格式
ssl_prefer_server_ciphers on;  ##依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码.
ssl_ciphers ALL:!ADH:!EXPORT56:-RC4+RSA:+HIGH:+MEDIUM:!EXP; ##指出为建立安全连接,服务器所允许的密码格式列表,密码指定为OpenSSL支持的格式
最近发表
标签列表