修改配置文件后执行命令重新加载配置(根据自己的安装目录):    /usr/local/nginx/sbin/nginx -s reload

#下载(root用户安装)
wget http://nginx.org/download/nginx-1.16.1.tar.gz

## 解压 tar
-zxvf nginx-1.16.1.tar.gz ##进入nginx目录 cd nginx-1.16.1 ## 配置 ./configure --prefix=/usr/local/nginx # make make make install

# cd到刚才配置的安装目录/usr/loca/nginx/
./sbin/nginx -t

#启动nginx
./sbin/nginx start

外网访问http://ip:80 验证是否启动成功


如果不行的话可以添加防火墙端口开放

开端口命令:firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙:systemctl restart firewalld.service

命令含义:

--zone #作用域

--add-port=80/tcp #添加端口,格式为:端口/通讯协议

--permanent #永久生效,没有此参数重启后失效

 

 

nginx的配置(示例) http配置

 

server {
  server_name confuelunce 
  listen 8090;
  charset utf-8;
  access_log  /var/log/nginx/confluence.access.log  main;
  location / {
     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_connect_timeout 1s;
        #禁用缓存
        proxy_buffering off;
        proxy_pass http://127.0.0.1:8090;

  }
  error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}




#upstaream配置如下

upstream test_api {
server 127.0.0.1:8180;
}

 

配置tcp协议

 

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}


#stream配置
stream {
   upstream tpctest{
     server 47.56.132.234:1080;
   }

    server {
       listen 7777;
       proxy_connect_timeout 1s;
       proxy_timeout 3s;
       proxy_pass teptest;
    }
}

 

错误1:运行后有waring告警:[warn] conflicting server name "listen" on 0.0.0.0:80, ignored

原因是:

nginx.conf的 server配置中"server_name confuelunce" 后面少了分号,

修改为 "server_name confuelunce ;" (注意不包含引号) 再次执行 /usr/local/nginx/sbin/nginx -s reload 发现没有告警了。

 

错误2:配置stream模块后加载配置报错(tcp的转发)

nginx: [emerg] unknown directive "stream" in /usr/local/nginx/conf/nginx.conf:259

 原因:

nginx默认安装的时候没有加载stream模块

需要重新对源文件进行编译、安装,通过添加--with-stream参数指定安装stream模块

./configure --with-stream

make & make install  (注意会覆盖原有nginx配置) 请参考另外一篇文章:https://blog.csdn.net/skate6/article/details/53929541

再次检查nginx.conf配置文件,确认配置无语法错误后,再次尝试启动服务。

nginx -t 检查配置文件是否正确

nginx -c 指定启动的配置文件

 

备注:nginx无法直接绑定域名进行tcp转发。如果想要通过一个域名绑定多个ip和端口需要在域名服务商(例如:阿里云控制台)将一个域名绑定到一个nignx server,然后在nginx server中配置多个tcp 的ip:port 。tcp访问的时候直接访问 nginxserver-ip:port 即可。

 

posted on 2020-01-02 15:16  张释文  阅读(263)  评论(1编辑  收藏  举报