nginx基本使用
程序位置:/usr/local/nginx/sbin/nginx
配置文件位置:/usr/local/nginx/conf/nginx.conf

启动nginx:
#systemctl start nginx

如果运行的时候不带-c参数,那就采用默认的配置文件,即/etc/nginx/nginx.conf

查看运行进程状态:
# ps aux | grep nginx

打开浏览器,访问http://localhost/看看nginx的默认页面:
请输入图片描述

停止nginx:
#systemctl stop nginx
    
重启nginx(配置文件变动后需要重启才能生效):
#systemctl restart nginx
    
检查配置文件是否正确:
# nginx -t
       
查看nginx版本
# nginx -v
    
回头看编译配置
# nginx -V

4、Nginx配置

#vi /etc/nginx/nginx.conf
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;

  sendfile on;

  keepalive_timeout 65;

  server {
    listen 80;

    server_name localhost;

    location / {
      proxy_pass http://192.168.5.147:8080;
    }

  }
}

按上面这样配置按理应该可以访问http://192.168.5.147 显示的应该是147:8080的网页内容,但 就是报错了

于是,这才查看日志

#vi /var/log/nginx/error.log
2017/11/03 05:23:53 [crit] 1331#1331: *12 connect() to 192.168.5.147:8080 failed 
(13: Permission denied) while connecting to upstream, client: 192.168.5.65, server: localhost,
request: "GET / HTTP/1.1", upstream: "http://192.168.5.147:8080/", host: "192.168.5.147"

园子中搜“(13: Permission denied) while connecting to upstream, client:”就找到原因,

type=AVC msg=audit(1509701033.988:119): avc: denied { name_connect } for pid=1331 
comm="nginx" dest=8080 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:http_cache_port_t:s0 tclass=tcp_socket

处理:

#setsebool -P httpd_can_network_connect 1

到这里应该就正常了,反向代理的后面设置
下面是设置两个服务器:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    
    upstream mycluster{
        #集群有几台服务器即可配置几台,ip_hash表示每个请求按访问ip的hash结果分配
         ip_hash;
         server 192.168.8.225:8080 ;
         server 192.168.8.231:8080 ;
    }
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  192.168.8.221;
        charset    utf-8;
       # root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
           proxy_next_upstream http_502 http_504 error timeout invalid_header;
       proxy_pass http://mycluster;
           
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}
最后修改:2020 年 11 月 17 日 02 : 09 PM
如果觉得我的文章对你有用,请随意赞赏