nginx代理 1个端口+路径匹配 代理多个web

目录

    • nginx安装及卸载
    • 修改配置文件
    • 注意事项
    • 记忆例子

nginx用yum安装在主机里,之前发过文章是装进容器里的,目录上稍有区别,配置文件基本一致,在主机内用的root用户,我部署nginx的主机地址是192.168.100.11,端口用的28080

nginx安装及卸载

1、nginx安装
用以下指令安装之后,就可以直接用了,如果有firewalld,可以选择关闭,也可以选择做端口放行

[root@localhost ~]# yum -y install epel-release #安装epel源
[root@localhost ~]# yum -y install nginx #安装最新版nginx
[root@localhost ~]# nginx -v #查看版本
[root@localhost ~]# systemctl start nginx #启动nginx

2、nginx使用过程中可能用到的指令

systemctl start nginx #启动nginx
systemctl stop nginx #关闭nginx
systemctl restart nginx #重启nginx
systemctl enable nginx #设置开机自启动
systemctl disable nginx #关闭开机自启动
nginx -t #检查配置文件
nginx -s reload #重载
systemctl start firewalld #开启闭防火墙
systemctl stop firewalld #关闭防火墙
systemctl enable firewalld #永久开启闭防火墙
systemctl disable firewalld #永久关闭防火墙
firewall-cmd --zone=public --permanent --add-port=80/tcp #防火墙永久放行80端口
firewall-cmd --zone=public --permanent --remove-port=80/tcp #防火墙永久关闭放行80端口
firewall-cmd --reload #重载

3、nginx卸载

[root@localhost ~]# systemctl stop nginx #关闭nginx
[root@localhost ~]# systemctl disable nginx #关闭开机自启动
[root@localhost ~]# find / -name nginx #查找nginx相关路径,然后用 rm -rf 删除
[root@localhost ~]# rm -rf /etc/nginx #举例删除,用不好的话,不能随便使用这个指令
[root@localhost ~]# yum -y remove nginx #卸载nginx

修改配置文件

因为我们的程序里有websocket,所以配置文件里增加了一点东西,在文件中标注了“ #看这里”字样的行仔细看看就行

[root@localhost ~]# vim /etc/nginx/nginx.conf #编辑配置文件

# 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/doc/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 4096;

    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;
    map $http_upgrade $connection_upgrade { # htpp升级为ws用
            default upgrade;
            #''   close;
    }
    server {
        listen       28080; #看这里
        listen       [::]:28080; #看这里
        server_name  _;
        root         /usr/share/nginx/html;

        location / { #看这里
            proxy_pass http://192.168.100.11:8080; #看这里
        } #看这里

        location /aj { # htpp升级为ws用
            proxy_pass http://192.168.100.11:8888;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        location /dyz { #看这里
            proxy_pass http://192.168.100.19:8001/;  #看这里
        } #看这里

        location /multipower/ {
            proxy_pass http://192.168.10.23:80; #看这里
        }
        
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

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

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

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        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;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

注意事项

nginx主机地址192.168.100.11,端口28080
配置文件中代理配置的两个部分有区别,我建议通过路径转发的话,路径后边加上“/”,更容易理解;

location /dyz {
    proxy_pass http://192.168.100.19:8001/;
}
# 第1个location部分,我写的“/dyz” ,“proxy_pass http://192.168.100.19:8001/;” ,在8001后边不带“/”,这个代理就有问题,就实现不了,这里这个问题研究了一个多小时才解决
#效果如下:
http://192.168.100.19:8001/login.html 等效于 http://192.168.100.11:28080/dyz/login.html
location /multipower/ {
    proxy_pass http://192.168.10.23:80;
}
# 第2个location部分,我写的“/multipower/” ,“proxy_pass http://192.168.10.23:80;” ,

# 如果在80后边不带“/” ,那么“/multipower/”就会补全到url的80端口末尾处,效果如下:
http://192.168.100.23:80/multipower/login.html 等效于 http://192.168.100.11:28080/multipower/login.html

# 如果在80后边带“/” ,那么“/multipower/”就不会补全到url的80端口末尾处,效果如下:
http://192.168.100.23:80/multipower/login.html 等效于 http://192.168.100.11:28080/login.html

记忆例子

以下是帮助我个人的记忆方法,注意带与不带 “/”
写法 1:(路径追加至末尾)
location /multipower/ {
proxy_pass http://192.168.10.23:80;
}
http://192.168.100.11:28080/multipower/login.html
代理到 http://192.168.10.23:80/multipower/login.html

写法 2:(路径后的内容替换,“/multipower/*” 与“/192.168.10.23:80/*”)
location /multipower/ {
proxy_pass http://192.168.10.23:80/;
}
http://192.168.100.11:28080/multipower/login.html
代理到 http://192.168.10.23:80/login.html

写法 3:(路径内容替换,“/test/” 与 “/multipower/”)
location /test/ { # 其中 test 也可以写成 multipower ,这样效果和写法1效果一样
proxy_pass http://192.168.10.23:80/multipower/;
}
http://192.168.100.11:28080/test/login.html
代理到 http://192.168.10.23:80/multipower/login.html

写法 4:(路径内容替换成空的,“/multipower” 与 “192.168.10.23:80/*”)
location /multipower {
proxy_pass http://192.168.10.23:80/;
}
http://192.168.100.11:28080/multipower/login.html
代理到 http://192.168.10.23:80/login.html

写法5:(需要死记的例子)
location /multipower {
proxy_pass http://192.168.10.23:80/aaa;
}
http://192.168.100.11:28080/multipower/login.html
代理到 http://192.168.10.23:80/aaalogin.html

写完脑子有点乱,要是有错得地方,求帮忙指正

你可能感兴趣的:(linux应用程序,nginx,运维,linux)