nginx学习
1、centos7下安装nginx
首先去nginx官网https://nginx.org/下载对应的版本
安装nginx所需要的依赖
yum -y install pcre-devel
yum -y install openssl openssl-devel
解压nginx压缩包
tar -zxvf nginx-1.18.0.tar.gz
进入解压后的目录,进行检查、编译、安装,执行如下命令:
./configure
make
make install
2、nginx启动、关闭、重启
配置文件在conf目录下的nginx.conf文件
静态页面为html目录下的静态页面
#进入sbin目录下
./nginx #启动
./nginx -s reload #重新加载配置
./nginx -s stop #关闭
./nginx -s quit #优雅地退出
3、nginx配置反向代理
参考的配置文件
#参考的完整配置文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#改配置为配置一个项目的情况下配置反向代理
server {
listen 80; #监听的端口
server_name localhost; #服务器名称
location ~ /mytest/ { #当请求路径包含有/mytest/的时候会请求http://192.168.17.128:8080
proxy_pass http://192.168.17.128:8080;
}
#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 html; #错误页面在当前配置文件的上一级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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
配置两个项目:
配置的监听本地的端口为80,服务器名字为本地的ip地址
当请求路径中包含/m/,就会去请求http://192.168.17.1:8080;
当请求路径中包含/n/,就会去请求http://192.168.17.1:8081;
创建两个springboot项目
端口号为8081
端口号为8080
centos7防火墙
systemctl status firewalld #防火墙状态
systemctl stop firewalld #关闭防火墙
systemctl start firewalld #开启防火墙
firewall-cmd --list-all #查看开启的端口号
firewall-cmd --list-ports #查看开启的端口号
firewall-cmd --zone=public --add-port=8080/tcp --permanent #开启端口
参数含义:
--zone #作用域
--add-port=80/tcp #添加端口,格式为:端口/通讯协议
--permanent #永久生效,没有此参数重启后失效