nginx 安装搭建及常用命令

107 阅读4分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

1. linux下安装nginx

按照菜鸟教程搭建,但是安装nginx的时候需要根据自己需求选择版本,最好选择新版本 pcre-config --version: 8.35 nginx/1.17.3

菜鸟教程:www.runoob.com/linux/nginx…

2. 搭建静态网页

#user  nobody;
worker_processes  1;
 
events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
 
    server {
        listen       8087;
        server_name  localhost;
 
       location / {
            root   html;
            index  ownIndex.html ownIndex.htm;  
            # 改为自己的静态网页,并在usr/local/nginx/html下新建自己的ownIndex.html
        }
 
        location /dist/ {
         alias /usr/local/nginx/dist/;
        #autoindex on;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
     
    }
 
}

在浏览器访问ip地址即可显示静态ownIndex.html

3. 搭建项目

#user  nobody;
worker_processes  1;
 
events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    #gzip  on;
 
    server {
        listen       8087;
        server_name  localhost;
 
       # location / {
        #    root   html;
         #   index  index.html index.htm;
       # }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location /compete {
                    proxy_pass http://localhost:8081/demo;
         }
    }
 
}

访问路径为:http://ip:8087/compete 就相当于访问了 http://ip:8081/demo;

4. nginx 负载均衡

http{
.....
.... 
 #gzip  on;
 upstream competeserver{   #注意:这里的名字不要写成compete_server,不被识别
 
       server localhost:8081 weight=9; #weight:权重,可去掉
 
       server localhost:8082 weight=1;
  }
 
server{
   listen       80;
   server_name  localhost;
   .......
   .......
 
  location /compete {
 
             proxy_pass http://competeserver/ta404;
 
             #proxy_pass http://localhost:8081/ta404;
 
            }
 }
}

(如果想要支持下划线的话,需要增加如下配置:derscores_in_headers on; 未测试)

负载均衡的时候默认是轮询的(即第一请求访问服务器8081,第二次请求服务器8082),若没做session共享,则你第一次登录的请求在8081,进入首页后请求8082,8082发现没有session,则把你当做是未登录,又跳回登录页。 要解决这个问题,这配置负载均衡的时候,不轮询,让同一ip请求同一服务器,加入配置ip_hash;

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的题目。

upstream competeserver{   
      ip_hash;
 
       server localhost:8081 weight=9; #weight:权重,可去掉
 
       server localhost:8082 weight=1;
  }

Nginx不仅可以作为一个Web服务器或反向代理服务器,还可以通过upstream指令实现配置负载均衡服务器组实现多种方式的负载均衡。4种典型方式:轮询方式、权重方式、ip_hash方式、第三方模块方式

nginx代理tcp,nginx代理redis:

1.需要安装 --with-stream模块

./configure --with-stream

2.在http同级添加stream

http { ...... }

注:stream和http在相同级别

stream {    
    upstream redis {
        server 127.0.0.1:6000;
        server 127.0.0.1:6001;
    }
    server {
        listen 7000;
        #proxy_pass不要加http://
        proxy_pass redis;
    }
}

5. nginx 负载均衡

查看请求被转发到哪台服务器 添加如下配置:

add_header backendIP $upstream_addr;
add_header backendCode $upstream_status;
location /compete {
 
             proxy_pass http://competeserver/ta404;
 
             #proxy_pass http://localhost:8081/ta404;
 
           add_header backendIP $upstream_addr;
       add_header backendCode $upstream_status;
            }

在浏览器请求后,在request的Headers信息中,可以看到当前负载到服务器的真实ip和端口(backendIP、backendCode)

6. nginx支持websocket

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600s;

其中第一行是告诉nginx使用HTTP/1.1通信协议,这是websoket必须要使用的协议。

第二行和第三行告诉nginx,当它想要使用WebSocket时,响应http升级请求。

第四行:不添加第四行:websocket报异常:EOFException,WebSocket走了OnError方法,断开了连接,因为是使用的Nginx做代理,怀疑是Nginx将连接断开了,nginx默认60秒断开连接,尝试设置nginx的断连时间更长一点比如1小时(3600s)。

7. 常用命令

nginx是否安装:nginx -v   (该命令查看nginx版本,若存在则安装)或者 
/usr/local/nginx/sbin/nginx -v
 
启动nginx:
 
/usr/local/nginx/sbin/nginx 或者 cd /usr/local/nginx/sbin ./nginx
 
检查配置文件是否有效:
 
/usr/local/nginx/sbin/nginx -t 或 cd /usr/local/nginx/sbin ./nginx -t
 
重新加载配置文件:
 
/usr/local/nginx/sbin/nginx -s reload
 
停止nginx:
 
/usr/local/nginx/sbin/nginx -s stop 或者 cd /usr/local/nginx/sbin ./nginx -s stop

8. 常见问题

1.centos启动Nginx提示nginx:

[emerg] still could not bind():

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

查看端口占用情况: netstat -ntlp | grep 80

tcp6       0      0 :::80                   :::*                    LISTEN      31101/java 

若要终止该进程,可使用kill命令 kill -9 31101

当然也可以不使用默认的80端口,修改nginx监听端口,使用未占用端口,比如8087、8088...

  1. 端口访问不到

考虑是否开启防火墙,导致端口访问不到