一、安装nginx前

我们首先要确保系统安装了g++、gcc、openssl-devel、pcre-devel和zlib-devel软件,可通过如图所示命令进行检测,如果以安装我们可以通过图二所示卸载:

yum install gcc-c++
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

## 如果已安装,则remove掉
yum remove nginx

 

wget https://nginx.org/download/nginx-1.11.3.tar.gz

  我们一般安装linux软件都会在/usr/local目录下,然后进行解压编译安装:

tar -zxvf nginx-1.11.3.tar.gz

mv nginx-1.11.3 /usr/local/nginx-1.11.3
./configure --prefix=/usr/local/nginx
## 安装到/usr/local/nginx的nginx目录下
make
make install

  

此时安装成功:

linux yum 安装的nginx 升级 linux nginx重装_nginx

此时源码包和安装后的包都有了,进入到nginx目录下,(一般要不要修改源码包,再make make install,需要看修改什么)

linux yum 安装的nginx 升级 linux nginx重装_Access_02

 

如上,进入到sbin/nginx,启动即可。

二、配置和说明:

接下来介绍下启动/停止/重启的具体方法,进入目录后我们可以用执行sbin/nginx来启动,也可以通过conf/nginx.conf来启动,停止我们可以查询进程使用kill -9 进程号/pkill -9 nginx来结束nginx服务,重启可以通过 sbin/nginx -s reload来重启,具体命令大家请看如图所示

linux yum 安装的nginx 升级 linux nginx重装_Access_03

 三、nginx的开发实例

在nginx.conf文件增加代码

gzip  on;
include ../conf.d/*.conf;

这样就可以在conf.d文件夹下添加配置文件,例如

server{
     listen 80;
     server_name felab.**.com;
     root /home/project/RDFELabClient/;
}

/usr/local/nginx/sbin/nginx -t试下是否配置有问题

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successfu

  重启绑定host就可以正式访问了

 四、把nginx设置全局变量

ln -s /usr/local/nginx/sbin/nginx /usr/sbin/

/usr/local/nginx/sbin/nginx是nginx的启动命令

/usr/sbin/就是环境变量目录

  

五、如果修改的配置在执行发生下面的错误

  nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"或者修改配置不生效

service nginx restart #不生效

  尝试执行下

/usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

 

六、nginx的端口代理

server {
  listen 80;
  server_name fcdn.**.com;
  index index.html index.htm index.php;
  root /home/www/fcdn.**.com;

          location ~ .*\.(ttf|otf|eot|woff|woff2|svg)$ {
                      add_header Access-Control-Allow-Origin "*";
                  }

          location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
                            expires      30d;
                    add_header Access-Control-Allow-Origin "*";
                  }
 
          location ~ .*\.(js|css)?$ {
                            expires      12h;
                  }

          access_log  /home/nginx/logs/fcdn.xesimg.com.log main ;
   }

  

server {
    listen       80;                        # 监听端口
    server_name npm.xxx.com;    # 站点域名
    index index.html index.htm index.php;   # 默认导航页
      location / {
                    proxy_pass http://127.0.0.1:4873/;
                    proxy_redirect off;
                    proxy_set_header X-Real-IP $remote_addr;
                    #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    #以下是一些反向代理的配置,可选。
                    proxy_set_header Host $host;
                    client_max_body_size 10m;
                    client_body_buffer_size 128k;
        }
  
      location ~ ^/verdaccio/(.*)$ {
        proxy_pass http://127.0.0.1:4872/$1;
        proxy_set_header Host            $host:$server_port;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
  error_page 500 502 503 504  http://www.xxx.com/Errors.htm;
}

  

跨域的通用配置

location /pub/(.+) {
    if ($http_origin ~ <允许的域(正则匹配)>) {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Credentials' "true";
        if ($request_method = "OPTIONS") {
            add_header 'Access-Control-Max-Age' 86400;
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE';
            add_header 'Access-Control-Allow-Headers' 'x-real-ip, x-forwarded-ip, accept, content-type';
            add_header 'Content-Length' 0;
            add_header 'Content-Type' 'text/plain, charset=utf-8';
            return 204;
        }
    }
    # 正常nginx配置
    ......
}

 

 七、websocket的配置

如果报“failed: Error during WebSocket handshake: Unexpected response code: 400”错误,可能是nginx的配置关键参数没有。

server{
        listen 80;
        server_name fedata.xxx.com;
        location / {
                 proxy_pass    http://127.0.0.1:7002;
                 proxy_http_version 1.1;
                 proxy_set_header Upgrade $http_upgrade;
                 proxy_set_header Connection "Upgrade";
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
         }
}

代理webSocket的关键参数

  • proxy_set_header Upgrade 把代理时http请求头的Upgrade 设置为原来http请求的请求头,ws协议的请求头为websocket
  • proxy_set_header Connection 因为代理的ws协议,所以http请求头的Connection设置为Upgrade
  • proxy_set_header X-Real-IP 给代理设置原http请求的ip,填写$remote_addr 即可

至于websocket协议的response的参数,在反向代理的时候不用管。

到这里,Nginx反向代理webSocket的配置就完成了,重启Nginx,用websocket连接试试,如果websocket成功连接,说明Nginx反向代理websocket已经成功了.

 8、nginx 的upsteam实现负载

upstream app.**.com {
   server backend1.example.com     weight=5;
    server 127.0.0.1:8080           max_fails=3 fail_timeout=30s;
    server unix:/tmp/backend3;

    server backup1.example.com:8080 backup;
}

server {
      listen 80;
      server_name app.**.com;
      access_log /home/nginx/logs/app.xueersi.com_access.log main;
      error_page 500 502 503 504  http://www.xueersi.com/wait.html;
    #   include /home/openresty/nginx/conf/nconf/xueersissl.conf;
      location / {
          set $upstream 'app.**.com';
          proxy_pass http://$upstream;
          add_header 'Access-Control-Allow-Origin' '$http_origin';
          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_redirect off;
          client_max_body_size 500m;
          client_body_buffer_size 128k;
          proxy_ignore_client_abort on;
          proxy_connect_timeout 60;
          proxy_send_timeout 60;
          proxy_read_timeout 60;
          proxy_buffer_size 128k;
          proxy_buffers 32 32k;
          proxy_busy_buffers_size 128k;
          proxy_temp_file_write_size 128k;
          proxy_next_upstream off;
          add_header Xes-App $upstream_http_server;
      }
}

  weight=number设定服务器的权重,默认是1。max_fails=number设定Nginx与服务器通信的尝试失败的次数。在fail_timeout参数定义的时间段内,如果失败的次数达到此值,Nginx就认为服务器不可用。在下一个fail_timeout时间段,服务器不会再被尝试。 失败的尝试次数默认是1。设为0就会停止统计尝试次数,认为服务器是一直可用的。