• Linux下Nginx使用


    1. 安装

    CentOS 7上Nginx的安装和启动方法如下

    # yum install nginx
    
    # firewall-cmd --permanent --zone=public --add-service=http
    # firewall-cmd --permanent --zone=public --add-service=https
    # firewall-cmd --reload
    
    # systemctl start nginx
    # systemctl enable nginx

    安装完成后在浏览器中打开服务器对应的IP地址即可测试是否安装成功

    2. 配置

    Nginx的主配置文件是/etc/nginx/nginx.conf, 更详细的介绍参考文末

    3. 转发

    转发也叫正向代理, 以下的配置将所有http请求转发至www.example.com

    server {
            listen       80 default_server;
            listen       [::]:80 default_server;
            server_name  _;
            root         /usr/share/nginx/html;
    
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
    
            location / {
                resolver   114.114.114.114;
                proxy_pass http://www.example.com:80;
            }
    
            error_page 404 /404.html;
                location = /40x.html {
            }
    
            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
        }

    注意: Nginx不支持https正向代理.

    3. 路由

    章节二中把所有请求全部转发至另一个地址, 现在假如我们的服务器除了转发之外还要能够自行处理部分请求

    为了进行方便区分, 我们需要进行如下路由:  除了/forward/...进行转发外, 其他请求自己处理
    http://IP-A/forward/a/b/c  --->  http://IP-B/a/b/c

    配置如下

             location / {
            }
    
            location 
    ^~ /forward/
     {
                resolver   114.114.114.114;
                proxy_pass http://www.example.com:80/;
            }

    4. SSL配置

    假定域名已经申请到SSL证书, 下面的配置开启SSL支持

    server {
            listen       443 ssl http2 default_server;
            listen       [::]:443 ssl http2 default_server;
            server_name  www.example.com;
            root         /var/www
    
            ssl_certificate "/opt/certs/www.example.com_bundle.crt";
            ssl_certificate_key "/opt/certs/www.example.com.key";
            ssl_session_cache shared:SSL:1m;
            ssl_session_timeout  10m;
            ssl_ciphers HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers on;
    
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
    
            location / {
            }
    
            error_page 404 /404.html;
                location = /40x.html {
            }
    
            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
        }

    参考:
    <Nginx路由>
    <Nginx配置入门>
    <Nginx简介及配置文件详解>
    <Centos下Nginx安装与配置>
    <Nginx使用ssl模块配置HTTPS支持>
    <Nginx proxy pass路由转发简单用法>

  • 相关阅读:
    使用paramikoHelper类实现MySQL安装和数据恢复
    tornado-模板,转义,上传静态文件
    tornado-请求与响应
    tornado-输入
    tornado-输出,request
    配置Ubuntu虚拟环境
    tornado-简单的服务器非阻塞
    Linux查看进程,端口,访问url
    tornado-简单的服务器
    字符串,数组,定时器,form
  • 原文地址:https://www.cnblogs.com/hzl6255/p/7324438.html
Copyright © 2020-2023  润新知