nginx 学习

124 阅读1分钟

nginx 反向代理

服务器是 centos7,反向代理 tomcat 8080端口

docker 启动 tomcat/8.0.52

docker run -d -p 8080:8080 tomcat 访问地址 192.168.220.129:8080 显示 tomcat 主页面,如下

tomcat 访问首页可能会出现访问不到的情况,可能是 tomcat webapps 下无内容导致,比如 tomcat:7 这个版本就会出现这个问题。

nginx 配置

/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;

    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;
    server {
      listen       80; # 监听 192.168.220.129:80
      server_name  192.168.220.129;

      #charset koi8-r;
      #access_log  /var/log/nginx/host.access.log  main;

      location / {
          root   /usr/share/nginx/html;
          proxy_pass http://127.0.0.1:8080; # 转发到 8080
          index  index.html index.htm;
      }

      #error_page  404              /404.html;

      # redirect server error pages to the static page /50x.html
      #
      #error_page   500 502 503 504  /50x.html;
      #location = /50x.html {
      #    root   /usr/share/nginx/html;
      #}

      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      #    proxy_pass   http://127.0.0.1;
      #}

      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      #location ~ \.php$ {
      #    root           html;
      #    fastcgi_pass   127.0.0.1:9000;
      #    fastcgi_index  index.php;
      #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
      #    include        fastcgi_params;
      #}

      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      #    deny  all;
      #}
  	}

}
service nginx start
nginx -s reload
nginx -s stop

nginx 启动

service nginx start

ps -ef | grep nginx 查看状态

注意事项

检查端口是否开放

Centos7 开启 , 关闭端口

CentOS7 默认没有使用iptables , 所以不能通过编辑 iptables 的配置文件来开启端口 , CentOS7 采用了 firewalld 防火墙

如要查询是否开启3306端口则 :firewall-cmd --query-port=3306/tcp

开启端口 :firewall-cmd --zone=public --add-port=3306/tcp --permanent

命令含义 : --zone #作用域 , --add-port=80/tcp #添加端口 , 格式为 : 端口/通讯协议 , --permanent #永久生效 , 没有此参数重启后失效

重启防火墙 : firewall-cmd --reload

关闭端口 : firewall-cmd --zone=public --remove-port=3306/tcp --permanent

查看防火墙状态 :systemctl status firewalld

开启防火墙服务 :systemctl start firewalld.service

设置防火墙开机启动 :systemctl enable firewalld.service

关闭 selinux