nginx基本配置與使用--Ubuntu系統


本文使用的是ubuntu 18.04系統,使用apt-get 方式安裝nginx。

nginx啟動與重啟命令:

sudo service nginx start|restart

 

nginx的配置目錄在/etc/nginx,目錄中含如下文件:

 

 

1、nginx.conf文件

/etc/nginx/nginx.conf是nginx的核心配置文件,文件內容如下:

user www-data;
worker_processes auto; #此數值越大,nginx並發能力越強
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768; #此數值越大,nginx的並發能力越強
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;
 include /etc/nginx/mime.types; #引入外部文件,存放媒體類型
    default_type application/octet-stream; #默認使用的媒體類型

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log; #訪問日志地址
    error_log /var/log/nginx/error.log; #錯誤日志地址

    ##
    # Gzip Settings
    ##

    gzip on; #開啟gzip壓縮

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##
 include /etc/nginx/conf.d/*.conf; #引入外部文件,配置server塊(一般用於設置負載均衡)
    include /etc/nginx/sites-enabled/*; #引入外部文件,配置本機站點
}


#mail {
#    # See sample authentication script at:
#    # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#    # auth_http localhost/auth.php;
#    # pop3_capabilities "TOP" "USER";
#    # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#    server {
#        listen     localhost:110;
#        protocol   pop3;
#        proxy      on;
#    }
# 
#    server {
#        listen     localhost:143;
#        protocol   imap;
#        proxy      on;
#    }
#}

重點關注標紅的兩個目錄。

nginx的並發能力公式:

動態資源 = worker_processes * worker_connections / 4
靜態資源 = worker_processes * worker_connections / 2

 

2、/etc/nginx/sites-enable/default文件

 

 此文件夾下只有一個文件default,使用ls -l命令查看:

 

default文件是 /etc/nginx/sites-available/default文件的鏈接。

因此,修改這兩個文件的效果是相同的。

該default文件內容如下:

# Default server configuration
#
server {
    listen 80 default_server; #監聽80端口
    listen [::]:80 default_server;
 root /home/ubuntu/index/wwwroot; #靜態資源代理

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html; #默認首頁

    server_name _; #站點名稱

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
}

server {
    listen 8080; #監聽8080端口
    listen [::]:8080;

    server_name ddz; #站點名稱

    root /home/ubuntu/ddj/client/dist; #靜態資源代理
    index index.html; #默認首頁
  #autoindex on; #目錄瀏覽 location
/ { try_files $uri $uri/ =404; } } server { listen 8090; #監聽8090端口 listen [::]:8090; server_name chess; #站點名稱 root /home/ubuntu/chess/dist; #靜態資源代理 index index.html; #默認首頁 location / { try_files $uri $uri/ = 404; } } server { listen 8088; #監聽8088端口 listen [::]:8088; server_name eshop; #站點名稱 root /home/ubuntu/eshop/dist; #靜態資源代理 index index.html; #默認首頁 location / { try_files $uri $uri/ = 404; } }

上述配置了80、8080、8090、8088共四個端口。

 

3、/etc/nginx/conf.d/目錄下文件。

這個目錄下默認為空,如果需要新建文件,應該以.conf為結尾。

這些文件用於反向代理和負載均衡的設置。具體設置方式見這篇文章

/etc/nginx/conf.d/default.conf,文件格式如下:

upstream asen{#通過反向代理實現負載均衡
     server 127.0.0.1:8088;#真實端口
}

server {
     listen 8085; #反向代理服務器端口
     server_name  mynginx;#站點名稱

      location / {
             # 后端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP 
             proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header Host $http_host;
             proxy_set_header X_Nginx_Proxy true;
             proxy_pass http://asen; #動態資源代理
             proxy_redirect off;
      }
}

通過上述的設置,可以通過8085端口來訪問8088端口的站點,實現了反向代理。

 

正向代理:

1、正向代理是由客戶端設立的。

2、客戶端了解代理服務器和目標服務器都是誰。

3、作用是可以實現突破訪問權限,提高訪問速度,對目標服務器隱藏客戶端的ip地址。

 

反向代理:

1、反向代理服務器是配置在服務端的。

2、客戶端不知道具體訪問的是哪台服務器。

3、可以實現負載均衡的功能,並隱藏服務器真實ip地址。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM