前端安裝配置nginx服務器反向代理

前端安裝配置nginx服務器

1.安裝Homebrew

這個工具類似於 npm,就是第三方包安裝工具。我使用的 Mac 系統,在終端輸入

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 

安裝時間根據網絡情況確定,我需要大約20分鐘。其間提示輸入密碼,輸入Mac密碼,安裝成功後進行下一步操作。

2.安裝nginx

我們選擇依賴 Homebrew 安裝nginx可以減少很多麻煩,你會發現nginx安裝過程中下了很多東西.

只需要在終端輸入

brew install nginx

在這裏插入圖片描述
如果沒有報錯,下載完成

3.開啓nginx服務器

安裝好了,就可以啓動nginx了,終端輸入 brew services start nginx

如果遇到聯網選項,點擊允許
在這裏插入圖片描述

可以直接轉到瀏覽器輸入:

http://localhost:8080

在這裏插入圖片描述

如果出現上圖所示,表面nginx安裝並開啓成功.

4.關閉重啓nginx

nginx 其他命令(關閉重啓),把 start 替換即可

brew services stop nginx
brew services restart nginx

在這裏插入圖片描述

5.nginx配置簡單說明

nginx默認安裝到 /usr/local/ 目錄下

/usr/local/Cellar/nginx

具體的配置文件如下,我們可以複製一份

cp /usr/local/etc/nginx/nginx.conf

user root owner; 注意設置訪問權限( user root owner; ),不然等會訪問網站會出現403錯誤

#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;
    
    server {
        listen       8080;
        server_name  localhost;
    
        #charset koi8-r;
    
        #access_log  logs/host.access.log  main;
    
        location / {
            root   html;
            index  index.html index.htm;
        }
    
        #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
    # 這裏可以自定義端口
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    
    #    location / {
    #        root   html; 本地網站文件夾的路徑
    #        index  index.html index.htm; 設置默認的網頁
    #    }
    #}


    # 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;
    #    }
    #}
    include servers/*;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章