zoukankan      html  css  js  c++  java
  • ## nginx 使用

    nginx 使用

    一、概述

    有一个域名,有一台服务器,有多个服务。
    为了可以通过域名来访问 服务器上的不同服务,而不将端口 对外 暴露。 采用 nginx 来进行 转发。

    二、nginx 安装

    不同的操作系统上,需要安装不同的包。
    这里以在 centos6.5 上安装为例。
    官网下载 最新的稳定包。 官网提供的包 需要自行进行 编译。
    我想要将第三方包放在 当前我指定的目录下,比如 ~/share

    Nginx 安装步骤

    系统平台:CentOS release 6.6 (Final) 64位。

    • 1 安装编译工具及库文件
    yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
    
    • 2 要安装 PCRE
      PCRE 作用是让 Nginx 支持 Rewrite 功能。
    # 1、下载 PCRE 安装包
    [root@master ~]# cd ~/share
    $ wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
    
    # 2、解压安装包:
    [root@master share]# tar zxvf pcre-8.35.tar.gz
    
    # 3、进入安装包目录
    [root@master share]# cd pcre-8.35
    
    # 4、编译安装 
    [root@master pcre-8.35]# ./configure
    [root@master pcre-8.35]# make && make install
    
    # 5、查看pcre版本
    [root@master pcre-8.35]# pcre-config --version
    
    • 3 安装 Nginx
    # 1、下载 Nginx,下载地址:http://nginx.org/download/nginx-1.16.1.tar.gz
    [root@master share]# cd ~/share
    [root@master share]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
    
    # 2、解压安装包
    [root@master share]# tar zxvf nginx-1.16.1.tar.gz
    
    # 3、进入安装包目录
    [root@master share]# cd 1.16.1
    
    # 4、编译安装
    [root@master nginx-1.16.1]# ./configure --prefix=/root/share/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/root/share/pcre-8.35
    [root@master nginx-1.16.1]# make
    [root@master nginx-1.16.1]# make install
    
    # 5、查看nginx版本
    [root@bogon nginx-1.16.1]# /root/share/nginx/sbin/nginx -v
    # 到此,nginx安装完成。
    

    你将会发现 产生了一个 /root/share/nginx 目录,这就是编译后的nginx 运行包,如果你还有其他同类机器需要安装 nginx,你只需要将此文件夹,移植到其他机器即可。
    注意 需要设置 环境变量

    • 4 编写启停脚本

    为了运行nginx 的方便,可以来编写 nginx.sh 脚本来控制它的启动停止
    为了nginx 能自动重启,可以配置 nginx.service 放在 /lib/systemd/system/ 目录下

    # nignx.service 的内容
    [Unit]
    Description=nginx
    After=network.target
    
    [Service]
    Type=forking
    ExecStart=/root/share/nginx/sbin/nginx.sh start
    ExecReload=/root/share/nginx/sbin/nginx.sh restart
    ExecStop=/root/share/nginx/sbin/nginx.sh stop
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    

    详情参考:http://www.seekl.net/

    三、nginx 配置

    1. Nginx同一个域名配置多个项目

    使用nginx 在同一域名在 配置多个项目 有两种方式:

      1. nginx 按不同的目录发给不同的项目
      1. 启用 二级域名,不同的项目分片不同的域名
    2.1 nginx 按不同的目录发给不同的项目
    server {
        listen    80;
        server_name example.com;
    
        location ^~ /project1 {
            proxy_pass     http://localhost:8081;
            proxy_set_header  Host       $host;
            proxy_set_header  X-Real-IP    $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    
        location ^~ /project2 {
            proxy_pass     http://localhost:8082;
            proxy_set_header  Host       $host;
            proxy_set_header  X-Real-IP    $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    
        location / {
           proxy_pass     http://localhost:8080;
           proxy_set_header  Host       $host;
           proxy_set_header  X-Real-IP    $remote_addr;
           proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    

    这里配置了三个项目:

    http://example.com/project1路径分发到http://localhost:8081
    http://example.com/project2路径分发到http://localhost:8082
    其他路径分发到http://localhost:8080

    2.2 启用 二级域名,不同的项目分片不同的域名
    server {
        listen    80;
        server_name example.com;
        location / {
           proxy_pass     http://localhost:8080;
           proxy_set_header  Host       $host;
           proxy_set_header  X-Real-IP    $remote_addr;
           proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    # project1
    
    server {
        listen    80;
        server_name project1.example.com;
        location / {
           proxy_pass     http://localhost:8081;
           proxy_set_header  Host       $host;
           proxy_set_header  X-Real-IP    $remote_addr;
           proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    # project2
    
    server {
        listen    80;
        server_name project2.example.com;
        location / {
           proxy_pass     http://localhost:8082;
           proxy_set_header  Host       $host;
           proxy_set_header  X-Real-IP    $remote_addr;
           proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    注意:这三个项目属于不同的域名,项目之间通过http访问会存在跨域问题。
    
    2.3 编写实际配置

    根据我们的项目实际情况分析,应 采用 不同域名来访问

    # 编写 nginx.conf 的配置,将其中引入多个配置文件
    include vhost/*.conf;
    
    

    四、nginx 常用命令

    nginx -t   # 校验配置文件的正确性
    nginx                      # 启动nginx
    nginx -s reload            # 重新载入配置文件
    nginx -s reopen            # 重启 Nginx
    nginx -s stop              # 停止 Nginx
    

    五、nginx 安全

    5.1 nginx 的跨域访问

    当浏览器发起ajax请求到其他域名时,会出现跨域的问题,在nginx上的解决方案是配置Access-Control-Allow-Origin来解决,此参数只允许配置单个域名或者*,当我们需要允许多个域名跨域访问时却不好配置,可以用map来实现

    map $http_origin $corsHost {
        default 0;
        "~http://blog.panpanie.com" http://blog.panpanie.com;
        "~http://admin.panpanie.com" http://doc.panpanie.com;
        "~http://www.panpanie.com" http://www.panpanie.com;
    }
    server
    {
        listen 80;
        server_name search.panpanie.com;
        root /nginx;
        location /
        {
            add_header Access-Control-Allow-Origin $corsHost;
        }
    }
    

    5.2 打开日志发现报错Permission denied

      1. 启动用户与nginx 工作用户不一致
    [root@master nginx]# ps -ef | grep nginx
    root     25604     1  0 17:32 ?        00:00:00 nginx: master process /root/share/nginx/sbin/nginx -p /root/share/nginx -c ./conf/nginx.conf
    nobody   25605 25604  0 17:32 ?        00:00:00 nginx: worker process
    root     25743  7036  0 17:32 pts/1    00:00:00 grep --color=auto nginx
    
    
      1. 修改 conf/nginx.conf 的user改为和启动用户一致
    # vi conf/nginx.conf
    user root
    # 保存 :x
    

    5.3 nignx 配置 valid_referer

    为了配置一定的安全策略,将不允许 来自某些 域名的访问
    valid_referers none blocked server_names *.panpanie.com;
    if ($invalid_referer) {
    rewrite ^/ http://www.panpanie.com/ redirect ;
    }

  • 相关阅读:
    angluarjs2项目生成内容合并到asp.net mvc4项目中一起发布
    asp.net core 1.1 升级后,操作mysql出错的解决办法。
    asp.net core 简单部署
    asp.net core 简单部署之FTP配置(CentOS 7.0安装配置Vsftp服务器)
    Angular2中对ASP.NET MVC跨域访问
    js中获取DOM元素
    nodejs中的express框架
    jquery.validate的效验方式
    Asp.net MVC4 下二级联动
    Newtonsoft.Json文件错误
  • 原文地址:https://www.cnblogs.com/panie2015/p/12198240.html
Copyright © 2011-2022 走看看