如何將自己的項目部署到阿里云服務(wù)器之【Nginx 來提供 HTTP 服務(wù)或者設(shè)置代理】

參考Centos7安裝Nginx實戰(zhàn),寫的非常詳細(xì)。

1.查看Nginx所有版本:http://nginx.org/download/

nginx.png

2.我選用的是nginx-1.18.0.tar.gz 這個包

3.Nginx基本使用方式:

(1).進(jìn)入安裝目錄

cd /usr/local/nginx/sbin

(2).啟動

./nginx

(3).關(guān)閉 nginx

./nginx -s stop

(4).重啟 nginx

./nginx -s reload

(5).Nginx在啟動時默認(rèn)加載conf/nginx.conf文件,這個文件就是nginx的配置文件了,我給出我的配置:

cd /usr/local/nginx/conf
vim nginx.conf
#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;
    # 這里監(jiān)聽 80端口,默認(rèn)打開前臺頁面
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/blog/front/web/dist/; # 這個是我前臺前端打包后的文件放置的位置,根據(jù)自己的修改。
            index  index.html index.htm; # 加載dist目錄下的index.html頁面
            try_files $uri $uri/ @router;
            autoindex on;
        }
        location /api/ { 
            # 這里是接口代理 比如你前臺頁面接口請求地址為:/api/login,那么會代理成http://127.0.0.1:7001/api/login。注意:proxy_pass 配置的http://127.0.0.1:7001后面沒有/。
            proxy_pass http://127.0.0.1:7001; # 這個地址是我后臺服務(wù)啟動后的地址
            proxy_set_header Host $host:$server_port;
        }
        location @router{
        # 這里配置是因為我們的項目是Vue或React通過js渲染出來的。默認(rèn)打包的只要index.html。所有需要做如下的路由配置。
            rewrite ^.*$ /index.html last;
        }
        gzip on;

        gzip_buffers 32 4k;

        gzip_comp_level 6;

        gzip_min_length 200;

        gzip_types text/css text/xml application/javascript;

        gzip_vary on;

        #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;
        }

        # 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
    # 管理后臺服務(wù)代理
    server {
        listen       9000;
        server_name  localhost;

        location / {
            root   /usr/blog/front/admin/dist/;
            index  index.html index.htm;
            try_files $uri $uri/ @router;
            autoindex on;
        }
        location /api/v1/ {
            proxy_pass http://127.0.0.1:7001;
            proxy_set_header Host $host:$server_port;
        } 
        location @router{
            rewrite ^.*$ /index.html last;
        }
        gzip on;

        gzip_buffers 32 4k;

        gzip_comp_level 6;

        gzip_min_length 200;

        gzip_types text/css text/xml application/javascript;

        gzip_vary on;

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


    # 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;
    #    }
    #}

}

我是開了兩個代理的:

前臺展示打開的服務(wù)代理和管理后臺打開的服務(wù)代理,這個項目是分開端口訪問的。 比如:我的公網(wǎng)IP是 47.134.30.148,那么可以通過 http://47.134.30.148 即可訪問前臺展示,http://47.134.30.148:9000 即可訪問管理后臺的登錄界面。

至于為什么要做如下配置:參考react,vue等部署單頁面項目時,訪問刷新出現(xiàn)404問題

try_files $uri $uri/ @router;

location @router{
    rewrite ^.*$ /index.html last;
}

建議仔細(xì)閱讀我上面提到的參考文章。里面的講解都是非常詳細(xì)的。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容