nginx配置文件实现vue重定向访问

1. nginx安装

    注:yum安装过程中如果报错except IOError, e invalid syntax并且重新安装了python3,那么是因为当前yum不支持python3,解决方案1:升级yum;2:修改/usr/bin/yum文件的开头改为#! /usr/bin/python2

yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
tar -zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0
./configure
make && make install
# 查看安装位置
whereis nginx
# 测试配置文件并检查配置文件 
nginx -t

如果报错error while loading shared libraries: libgd.so.2: cannot open shared object,需要安装libgd

yum install gd

当执行

nginx -t

出现

nginx: the configuration file /www/server/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /www/server/nginx/conf/nginx.conf test is successful

说明安装成功。

2. 配置conf文件

vi /www/server/nginx/conf/nginx.conf
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;
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 gupiao; # index index.html index.htm index.php; root /opt/monijizhang/app; # 对应的vue文件的路径,vue主文件index.html就在此文件夹下面 #error_page 404 /404.html; include enable-php.conf; location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /\. { deny all; } # 根请求会指向的页面 location / { # 此处的 @router 实际上是引用下面的转发,否则在 Vue 路由刷新时可能会抛出 404 try_files $uri $uri/ @router; # 请求指向的首页 index index.html; } # 由于路由的资源不一定是真实的路径,无法找到具体文件 # 所以需要将请求重写到 index.html 中,然后交给真正的 Vue 路由处理请求资源 location @router { rewrite ^.*$ /index.html last; } # 将所有的 http://****: 80/api/ 开头的请求都转发到下面 http://172.26.20.189:7979/api/中 # 这样就实现了前后端分离,前端请求由nginx直接指向vue文件,vue中的后端请求由nginx转发到后端的网址中 # 为了防止在访问页面时请求就被 Nginx 代理转发,这里需要更具体的配置,才能和前端访问请求区分开 location /api/ { # 后端的真实接口 proxy_pass http://172.26.20.189:7979/api/; # 改这里 proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Cookie $http_cookie; # for Ajax #fastcgi_param HTTP_X_REQUESTED_WITH $http_x_requested_with; proxy_set_header HTTP-X-REQUESTED-WITH $http_x_requested_with; proxy_set_header HTTP_X_REQUESTED_WITH $http_x_requested_with; proxy_set_header x-requested-with $http_x_requested_with; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 128k; proxy_buffers 32 32k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; }   ######这里往上####### access_log /www/wwwlogs/access.log; } include /www/server/panel/vhost/nginx/*.conf; }

开启防火墙端口

firewall-cmd --zone=public --add-port=7979/tcp --permanent
# 重载
firewall-cmd --reload

nginx重启

nginx -s reload

 

 

 

posted @ 2022-03-08 13:36  不要不说  阅读(456)  评论(0编辑  收藏  举报