记录使用nginx配置多端口时遇到的小插曲问题

user  www www;
worker_processes auto;
error_log  /www/wwwlogs/nginx_error.log  crit;
pid        /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

stream {
    log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';
  
    access_log /www/wwwlogs/tcp-access.log tcp_format;
    error_log /www/wwwlogs/tcp-error.log;
    include /www/server/panel/vhost/nginx/tcp/*.conf;
}

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }

http
    {
        include       mime.types;
		#include luawaf.conf;

		include proxy.conf;

        default_type  application/octet-stream;

        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        limit_conn_zone $binary_remote_addr zone=perip:10m;
		limit_conn_zone $server_name zone=perserver:10m;

        server_tokens off;
        access_log off;

server
    {
        listen 80;#端口
        server_name 139.x.xxx.xxx;#路由
        index index.html index.htm index.php;
        root  /www/server/phpmyadmin;
        
        #error_page   404   /404.html;
        include enable-php.conf;
        
        location /  {
            root  /home/test;#根目录
            index index.html;#默认页
        }
        
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
            root  /home/test;#静态资源目录
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /www/wwwlogs/access.log;
    }
    
include /www/server/panel/vhost/nginx/*.conf;
}

  例如上面是我的Nginx配置文件,对应我的资源目录如下:
在这里插入图片描述
  浏览器地址栏输入:139.x.xxx.xxx 访问成功

在这里插入图片描述

需求:

  我增加多了一个vue项目,希望在当前的服务器下(即同一个IP)配置不同的端口,也能实现访问。

user  www www;
worker_processes auto;
error_log  /www/wwwlogs/nginx_error.log  crit;
pid        /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

stream {
    log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';
  
    access_log /www/wwwlogs/tcp-access.log tcp_format;
    error_log /www/wwwlogs/tcp-error.log;
    include /www/server/panel/vhost/nginx/tcp/*.conf;
}

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }

http
    {
        include       mime.types;
		#include luawaf.conf;

		include proxy.conf;

        default_type  application/octet-stream;

        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        limit_conn_zone $binary_remote_addr zone=perip:10m;
		limit_conn_zone $server_name zone=perserver:10m;

        server_tokens off;
        access_log off;

server
    {
        listen 80;
        server_name 139.x.xxx.xxx;
        index index.html index.htm index.php;
        root  /www/server/phpmyadmin;
        
        #error_page   404   /404.html;
        include enable-php.conf;
        
        location /  {
            root  /home/test;
            index index.html;
        }
        
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            # expires      12h;
            root  /home/test;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /www/wwwlogs/access.log;
    }
    
    server
    {
        listen 8080;
        server_name 139.x.xxx.xxx;
        index index.html;
        root  /www/server/phpmyadmin;
        
        #error_page   404   /404.html;
        include enable-php.conf;
        
        location / {
            root  /home/choujiang/lucky-draw/dist;
            index index.html;
            proxy_pass http://localhost:8081;  # 替换为 Vue 项目实际运行的地址和端口
            proxy_set_header Host $host;
        }
        
        # # 在ip后面多加一级test目录访问
        # location /test{
        #     alias /home/choujiang/lucky-draw/dist;
        #     index favicon.ico;
        # }
    }
    
    

   
include /www/server/panel/vhost/nginx/*.conf;
}

  修改了我的Nginx配置文件,对应我的资源目录如下:

在这里插入图片描述

  这时浏览器地址栏输入:139.x.xxx.xxx:8080 访问不到,

在这里插入图片描述

  首先考虑到的是端口没有开放,于是乎去到服务器的安全组进行检查,发现8080端口和8081都是配置开放了的。

在这里插入图片描述
  如果你是个新手,刚接触Nginx,你肯定蒙圈了,这是什么原因引起的呢?60%的人会觉得是配置的问题,20%的人会觉的是端口是否被占用,还有10%的人觉得会不会是项目本身或者环境的问题,还有10%的人在尝试寻找其他方案。接着往下看,这时我们先以新手的身份来逐一排查。

  思路:(先确保我们的vue项目在本地是可以正常访问的)

  由于我们部署的是vue项目,可以先把vue进行构建好的dist文件上传到我们服务器的nginx的html目录下,即在服务器直接访问,看看能不能访问到项目

在这里插入图片描述在这里插入图片描述

  直接拷贝dist的内容复制到Nginx的html目录下,html里面的50x.html和index.html是本身存在,可以不用管,为例不和vue项目里面index.html冲突,先将html原来的index.html改名成index-副本.html。然后再去Nginx的配置文件中进行配置,配置如下:

user  www www;
worker_processes auto;
error_log  /www/wwwlogs/nginx_error.log  crit;
pid        /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

stream {
    log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';
  
    access_log /www/wwwlogs/tcp-access.log tcp_format;
    error_log /www/wwwlogs/tcp-error.log;
    include /www/server/panel/vhost/nginx/tcp/*.conf;
}

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }

http
    {
        include       mime.types;
		#include luawaf.conf;

		include proxy.conf;

        default_type  application/octet-stream;

        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        limit_conn_zone $binary_remote_addr zone=perip:10m;
		limit_conn_zone $server_name zone=perserver:10m;

        server_tokens off;
        access_log off;

server
    {
        listen 80;
        server_name 139.x.xxx.xxx;
        index index.html index.htm index.php;
        root  /www/server/phpmyadmin;
        
        #error_page   404   /404.html;
        include enable-php.conf;
        
        location /  {
            root  html;
            index index.html;
        }   
        
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
            root  html;
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /www/wwwlogs/access.log;
    }
    
include /www/server/panel/vhost/nginx/*.conf;
}

  发现这样子是可以访问到vue的项目的,那么大致就可以排出了之前的配置是没有问题的,项目也是没有问题的,最终只有一个可能,那就是防火墙没有关闭。接下来我们去检查服务器的防火墙。

  linux不同版本防火墙是不同的,一般linux 6.5 关于防火墙的命令如下

📌查看防火墙状态

service iptables status 

📌暂时关闭防火墙

service iptables stop

  linux 7.0以上默认安装firewalld

📌查看状态

systemctl status firewalld

在这里插入图片描述

  显示“active(running)”则说明防火墙是开着的

📌禁用防火墙

systemctl stop firewalld

  执行上面命令,将防火墙进行关闭

在这里插入图片描述

  查看状态,已经将防火墙关闭了

  接下来回到原来的配置,这次我们以888端口为例,进行配置:

user  www www;
worker_processes auto;
error_log  /www/wwwlogs/nginx_error.log  crit;
pid        /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

stream {
    log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';
  
    access_log /www/wwwlogs/tcp-access.log tcp_format;
    error_log /www/wwwlogs/tcp-error.log;
    include /www/server/panel/vhost/nginx/tcp/*.conf;
}

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }

http
    {
        include       mime.types;
		#include luawaf.conf;

		include proxy.conf;

        default_type  application/octet-stream;

        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        limit_conn_zone $binary_remote_addr zone=perip:10m;
		limit_conn_zone $server_name zone=perserver:10m;

        server_tokens off;
        access_log off;

server
    {
        listen 80;#端口
        server_name 139.x.xxx.xxx;#路由
        index index.html index.htm index.php;
        root  /www/server/phpmyadmin;
        
        #error_page   404   /404.html;
        include enable-php.conf;
        
        location /  {
            root  /home/test;#根目录
            index index.html;#默认页
        }
        
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
            root  /home/test;#静态资源目录
        }

        location ~ /\.
        {
            deny all;
        }

        access_log  /www/wwwlogs/access.log;
    }
    
    server
    {
        listen 888;
        server_name 139.x.xxx.xxx;
        index index.html;
        root  /www/server/phpmyadmin;
        
        #error_page   404   /404.html;
        include enable-php.conf;
        
        location / {
            root  /home/choujiang/lucky-draw/dist;
            index index.html;
        }
        
        location ~ .*\.(js|css)?$
        {
            expires      12h;
            root  /home/choujiang/lucky-draw/dist;#静态资源目录
        }
        access_log  /www/wwwlogs/access.log2;
    }
    
include /www/server/panel/vhost/nginx/*.conf;
}

  浏览器地址栏输入:139.x.xxx.xxx:888 访问成功

在这里插入图片描述

  对此,问题解决了,主要原因就是防火墙没有关闭。

附:

  如果想在地址栏多加一次目录访问,可以参考如下:

# 在ip后面多加一级test目录访问
# location /test{
#     alias /home/choujiang/lucky-draw/dist;
#     index index.html;
# }

  如果是请求后端,则需要配置一层代理

 location /  {
    root  html;
    index index.html;
    try_files $uri $uri/ /index.html;#history模式
}

location /api/ {  
    proxy_pass http://139.x.xxx.xxx:8080/;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值