受欢迎的博客标签

阿里云ubuntu14.04安装nginx详细过程全记录

Published

Table of Content

How To Install and uninstall reinstall Nginx on Ubuntu 18.04(aliyun)?

How to Install Tengine on Ubuntu 18.04 Step by Step?

 

 

差不多折腾了一天,才把阿里云ubuntu14.04安装 nginx和配置的事搞好。

查看操作系统版本号

# lsb_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 17.04
Release: 17.04
Codename: zesty

查看当前系统所用内核

# uname -r

4.10.0-19-generic

  一、nginx的安装

用root用户登录后,执行命令

#apt-get install nginx

apt-get是一个程序包下载和安装软件。

选Y,出现错误

Err http://mirrors.aliyun.com/ubuntu/ trusty-updates/main unzip amd64 6.0-9ubuntu1.1 404 Not Found [IP: 112.124.140.210 80] Err http://security.ubuntu.com/ubuntu/ trusty-security/main unzip amd64 6.0-9ubuntu1.1 404 Not Found [IP: 91.189.92.201 80]

E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/u/unzip/unzip_6.0-9ubuntu1.1_amd64.deb 404 Not Found [IP: 91.189.92.201 80]

问题原因

该问题通常是由于初始系统中的软件版本库与 apt 源中的版本不一致所致。即 apt-get 请求的资源在 apt 源中不存在。

解决办法

直接通过 apt-get update 进行源更新同步后,再进行apt-get install安装。 如果出现其他错误,看这篇文章:云服务器 ECS Ubuntu apt-get 在线安装软件和常见问题处理介绍,里面有多种错误的原因和解决办法介绍。没时间啰嗦,我们继续。。。。

#apt-get update 

#apt-get install nginx

安装成功了,不好意思,到了这一步,我都不知道安装的nginx版本号是多少,最新的稳定版? 试过下面的命令,但提示找不到版本号,版本号没错,照着nginx.org官网的版本号敲的。

#apt-get install nginx vesion=1.10.1  

Ubuntu安装之后的文件结构大致为:

所有的配置文件都在/etc/nginx下,并且每个虚拟主机已经安排在了/etc/nginx/sites-available下

程序文件在/usr/sbin/nginx

日志放在了/var/log/nginx中 并已经在/etc/init.d/下创建了启动脚本

nginx 默认的虚拟主机的目录设置在了/var/www/nginx-default (有的版本 默认的虚拟主机的目录设置在了/var/www, 请参考/etc/nginx/sites-available里的配置)

二、重装安装nginx 修改配置的时候折腾的改来改去,老是不成功,自己都不记得改了哪些地方了。按网上的操作先卸载再重装。各种方法都试了一遍,却发现ubuntu14.04没那么简单,所有的命令运行都正常,但/etc/nginx下就剩三个目录,正常的目录应该有7、8个。 问题 需要把ubuntu14.04的关于nginx的文件彻底删的干干净净,一个也不许剩下。把nginx的进程全部杀死,不能留一个活口。ubuntu14.04系统的世界清静了,才可以重装。ubuntu14.04是不是处女座的人开发的,有点洁癖啊。 具体操作看这篇:ubuntu14.04彻底删除nginx,我可是一个个命令、一个个字母敲的,因为不熟悉这个系统,不知道怎么偷懒,说眼睛不累,那是假的。我自己都佩服自己这种吃苦耐劳的老黄牛精神。如果生在国企,不给我评个全国五一劳模,那这个世界就真的太不公平了。

二、查看服务器上已安装的nginx版本号

ngix -v

 

ubuntu18.04X64完整安装nginx过程(run OK)

#apt-get update

#apt-get install nginx

choice y

 

 

三、安装vim

编辑器那个难用啊,想死。网上一查,别人也说烦死了。普京说:没有实力的愤怒毫无意义。好吧,老老实实查vi名令。关键点如下: 先按ESC,进入命令模式,简单操作按i进入编辑模式,一下不行就按两下,直到编辑器下面出现Insert字样,可以编辑。

esc退出编辑模式,

输入:可以输入命令,

wq是保存退出,q退出,!q不保存退出。这个还是网友告诉我的,我知道也就这么多了。 正常模式(按Esc或Ctrl+[进入) 左下角显示文件名或为空插入模式(按i键进入) 左下角显示--INSERT-- 退出命令 :wq 保存并退出 ZZ 保存并退出 :q! 强制退出并忽略所有更改 :e! 放弃所有修改,并打开原来文件。 鲁迅也说过一句话:不满是向上的车轮。这么难用,好多人不满,你丫的是不是也要考虑改进一下。  

Linux vi命令-vi编辑器入门教程

四、配置反向代理(ubuntu)

# vim /etc/nginx/nginx.conf

#http stard http {

... upstream proxy_server #这里是为了使用负载均衡,使得多个ip可以提供同一个服务,weight为权值,

server 10.10.12.203:8080 weight=2; # 权值越高,被选中的概率越大

server 10.10.12.202:8080 weight=4;

}

upstream hostproxy {  

     server 205.0.0.1:3431 max_fails=0 weight=1;

}

server {

listen 80;

server_name www.freeboygirl.com *.iaspnetcore.com; # 反向域名代理,不同的域名是指向同一入口ip,经过nginx,又转向不同的内部ip提供网络服务,"www"不省略  

location / {

     proxy_pass http://hostproxy;  

   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 end  

一个完整示例

#Node Express Staging on Port 3000

upstream appname{

server 127.0.0.1:3000;

}

# NGINX Server Instance,PORT 80 server

{

listen 0.0.0.0:80;

server_name appname.domain;

access_log /var/log/nginx/appname;

# Gzip Compression gzip on;

gzip_comp_level 6;

gzip_vary on; gzip_min_length 1000;

gzip_proxied any;

gzip_types text/plain text/css

application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; gzip_buffers 16 8k;

# Proxy to the Node instance

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_pass http://localhost:3000; proxy_redirect off; } }

六、重启nginx

#service nginx restart

result message: if  (display 。。。[OK]) then succesful if  (display 。。。[fail]) then 请继续折腾。。。

七、设置文件上传大小限制 打开nginx配置文件 nginx.conf, 路径:/etc/nginx/nginx.conf

在http{}段中加入

client_max_body_size 20m; 20m为允许最大上传的文件大小

http {

server_names_hash_bucket_size 64;

include mime.types;

default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent $request_body $upstream_http_content_type"$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; client_max_body_size 100m; #-----   }  

 八、nginx负载均衡设置  

upstream hostiaspnetcoreServer

{ server 127.0.0.1:8000 max_fails=0 weight=3; server 127.0.0.1:7999 max_fails=0 backup;}   server { #监听端口 listen 80; #自己指定要访问的域名 server_name *.iaspnetcore.com; #字符集 charset gb2312; #单独的access_log文件 access_log logs/www.iaspnetcore.com.access.log; #反向代理配置, #将所有请求为http://hostiaspnetcoreServer的请求全部转发到upstream中定义的目标服务器中。 location / { proxy_pass http://hostiaspnetcoreServer; #若给某一台服务器转发请求时,达到默认超时时间未响应,则再向另一台服务器转发请求。 proxy_connect_timeout 1; }       }  

九、nginx 多条件判断百度爬虫

nginx配置文件是不支持if嵌套、并且不支持类似shell 的 &&  和 || 的写法 ,必须想别的办法解决.变通一下,使用变量作为中间值,然后判断这个变量 set $baidumobilespider $http_user_agent;   set $flag "";   if ( $http_user_agent ~* "Baiduspider" ){ set $flag "${flag}1"; }if ( $http_user_agent ~* "Mobile" ){ set $flag "${flag}1"; }if ( $flag ~* "11" ){ set $baidumobilespider "Mozilla/5.0 (Linux;u;Android 4.2.2;zh-cn;) AppleWebKit/534.46 (KHTML,like Gecko) Version/5.1 Mobile Safari/10600.6.3 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)"; } proxy_set_header User-Agent $baidumobilespider;  

 http://www.cnblogs.com/taosim/articles/3291638.html http://www.cnblogs.com/linezero/p/aspnetcoreubuntu.html https://docs.asp.net/en/latest/publishing/linuxproduction.html#configure-a-reverse-proxy-server  

 Ubuntu14.04安装google浏览器

1. 第一步:下载google浏览器      

  1.32位下载地址:https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

2.64位下载地址:https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb 我的操作系统是64位的,于是下载64位软件包:wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

2. 第二步:安装下载好的软件包:

dpkg -i google-chrome-stable_current_amd64.deb

关于dpkg,其实有个更好的命令----gdebi,它能自动的解决依赖,尝试一下这个命令,也许你再也不想用dpkg –i了。  

 ubuntu 修改 ssh默认端口号 Linux中SSH默认端口为22,为了安全考虑,我们有必要对22端口进行修改,现修改端口为60000;

修改方法如下:

在/etc/ssh/sshd_config中

找到Port 22,将其修改为60000,或使用/usr/sbin/sshd -p 60000指定端口。

如果用户想让22和60000端口同时开放,只需在/etc/ssh/sshd_config增加一行内容如下:

[root@localhost /]# vi /etc/ssh/sshd_config

Port 22

Port 60000

保存并退出

[root@localhost /]#service ssh restart  

 十一、Windows nginx 平滑重启

step 1.首先,查看配置文件是否正确 nginx -t

step 2.重新载入Nginx配置 nginx.exe -s reload  

十二、为nginx 配置 ws

#ws start

proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection "Upgrade";

#ws end

see: https://www.nginx.com/blog/websocket-nginx/

  十三、 nginx 部署

.net core 获取的客户端ip为127.0.0.1采用nginx和.net core 部署一套api接口到服务器上,发现获取到的ip地址为127.0.0.1   经过检查发现,需要在nginx配置上以下参数 proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header REMOTE-HOST $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  

十四、输出到文本

ls>t.txt

python test.py | tee output.txt

 

upgrade nginx

$ sudo wget https://nginx.org/keys/nginx_signing.key
$ sudo apt-key add nginx_signing.key
$ sudo vi /etc/apt/sources.list
    deb https://nginx.org/packages/mainline/ubuntu/ bionic nginx
    deb-src https://nginx.org/packages/mainline/ubuntu/ bionic nginx
$ sudo apt-get remove nginx-common
$ sudo apt-get update
$ sudo apt-get install nginx

https://www.nginx.com/resources/wiki/start/topics/tutorials/install/

https://wiki.ubuntu.com/Releases

 

 

十四、错误记录

1.NGINX 反向代理错误:recv() failed (104: Connection reset by peer) while reading response header from upstream

问题描述:

项目中通过Nginx反向代理.net core服务.概率性出现502 Bad Gateway,出现概率较低.

解决办法:

(1)查看错误日志

vi /var/log/nginx/error.log

(2)错误信息

recv() failed (104: Connection reset by peer) while reading response header from upstream

(3)错误原因

具体原因:.net core 服务已经断开了连接,但是未通知到Nginx,Nginx还在该连接上收发数据,最终导致了该报错。

 

3.1

从client到nginx的连接

从nginx到server的连接

当使用nginx作为反向代理时,从HTTP协议的角度看,nginx在这个过程中,对于客户端它扮演着HTTP服务器端的角色。而对于真正的服务器端(在nginx的术语中称为upstream)nginx又扮演着HTTP客户端的角色。

为了支持长连接,需要做到两点:

从client到nginx的连接是长连接
从nginx到server的连接是长连接

 

服务端确实已经关闭了连接: upstream发送了RST,将连接重置。

      errno = 104 错误表明你在对一个对端socket已经关闭的的连接调用write或send方法,在这种情况下,调用write或send方法后,对端socket便会向本端socket发送一个RESET信号,在此之后如果继续执行write或send操作,就会得到errno为104,错误描述为connection reset by peer。

      如果对方socket已经执行了close的操作,本端socket还继续在这个连接上收发数据,就会触发对端socket发送RST报文。按照TCP的四次握手原理,这时候本端socket应该也要开始执行close的操作流程了,而不是接着收发数据。

Nginx 处理的每个请求均有相应的超时设置。如果做好这些超时时间的限定,判定超时后资源被释放,用来处理其他的请求,以此提升 Nginx 的性能。

see:https://www.cspsprotocol.com/tcp-connection-termination/

 

keepalive_timeout

HTTP 是一种无状态协议,客户端向服务器发送一个 TCP 请求,服务端响应完毕后断开连接。

如果客户端向服务器发送多个请求,每个请求都要建立各自独立的连接以传输数据。

HTTP 有一个 KeepAlive 模式,它告诉 webserver 在处理完一个请求后保持这个 TCP 连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。

KeepAlive 在一段时间内保持打开状态,它们会在这段时间内占用资源。占用过多就会影响性能。

Nginx 使用 keepalive_timeout 来指定 KeepAlive 的超时时间(timeout)。指定每个 TCP 连接最多可以保持多长时间。Nginx 的默认值是 75 秒,有些浏览器最多只保持 60 秒,所以可以设定为 60 秒。若将它设置为 0,就禁止了 keepalive 连接。

 

此问题在于 nginx的buffer太小,timeout太小。

 

添加 keepalive_timeout 240;

detail:https://blog.csdn.net/u014607184/article/details/107175596/

 

为了让nginx和server(nginx称为upstream)之间保持长连接,典型设置如下:

In order Nginx to keep connection alive, the following configuration is required:

(1)Configure appropriate headers (HTTP 1.1 and Connection header does not contain "Close" value, the actual value doesn't matter, Keep-alive or just an empty value)


(2)Use upstream block with keepalive instruction, just proxy_pass url won't work
(3)Origin server should have keep-alive enabled

So the following Nginx configuration makes keepalive working for you:

 

http {
    upstream  BACKEND {
        server   192.168.0.1:8080  weight=1 max_fails=2 fail_timeout=30s;
        server   192.168.0.2:8080  weight=1 max_fails=2 fail_timeout=30s;

        keepalive 300;        // 这个很重要!
    }

    server {
        listen 8080 default_server;
        server_name "";

        location /  {
            proxy_pass http://BACKEND;
            proxy_set_header Host  $Host;
            proxy_set_header x-forwarded-for $remote_addr;
            proxy_set_header X-Real-IP $remote_addr;
            add_header Cache-Control no-store;
            add_header Pragma  no-cache;

            proxy_http_version 1.1;                    // 这两个最好也设置
            proxy_set_header Connection "";

            client_max_body_size  3072k;
            client_body_buffer_size 128k;
        }
    }
}

http://nginx.org/r/keepalive

 

(4)修改nginx 配置参数

client_header_buffer_size 64k;
large_client_header_buffers 4 64k;
client_body_buffer_size 20m;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 256k;
gzip_buffers 16 8k;
proxy_buffer_size 64k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 256k;

keepalive_timeout 240;
fastcgi_connect_timeout 600;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;

proxy_connect_timeout 600s;
proxy_send_timeout 1200;
proxy_read_timeout 1200;

 

Backend Server

I have a .NET 5 web app running a Kestrel server behind nginx., the only thing I do is to set the log level to:

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace"
    }
  },
  "AllowedHosts": "*"
}

 

 

2. connect() failed (111: Connection refused) while connecting to upstream

原因:

出现这种问题的原因是因为NGinx代理的服务,upstram,或者直接pass_proxy的服务对应的端口没开放或者根本没有跑起来造成的,比如:我nginx代理转发另一台服务器的8080端口的API服务。但是这个API服务没有跑起来,或者API服务设置的127.0.0.1。都会造成这种情况。总之就是代理的服务端口没有对外开放造成的。

 

 

  


 

查找被占用的端口

netstat -tln
netstat -tln | grep 80

查看端口被哪个进程占用

lsof -i :80

杀掉占用80端口的进程

kill -9 进程id

 

错误处理

ubuntu14.04彻底删除nginx

https://blog.csdn.net/u010571844/article/details/50819704

Ubuntu查看和释放80端口

https://blog.csdn.net/licheetools/article/details/82946328

Ubuntu Linux: Start / Restart / Stop Nginx Web Server

https://www.cyberciti.biz/faq/nginx-restart-ubuntu-linux-command/

Nginx反向代理解决超长请求串post或get请求时,会返回413、400、414错误,post和get方式的解决办法

https://yq.aliyun.com/articles/114615

 

Nginx: Connection reset by peer 错误定位