1. 程式人生 > 其它 >nginx配置檔案實現vue重定向訪問

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