Nginx 配置

nginx 主配置文件

文件位置:  /etc/nginx/nginx.conf  

#For more information on configuration, see: # * Official English Documentation: http: //nginx.org/en/docs/
# * Official Russian Documentation: http: //nginx.org/ru/docs/

# 用戶
user nginx;
# 工做進程數量
worker_processes auto;

error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

#Load dynamic modules. See /usr/share/nginx/README.dynamic.
include/usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    # 開啓gzip壓縮
    gzip on;
    gzip_types
        text/plain
        text/css
        text/xml
        text/javascript
        application/javascript
        application/x-javascript
        application/json
        application/xml
        application/xml+rss
        application/xhtml+xml
        application/x-font-ttf
        application/x-font-opentype
        image/svg+xml
        image/x-icon;
}複製代碼

上面是主要配置文件,這裏把 server 的配置部分單獨拿出來,放到 /etc/nginx/nginx.conf/default.conf 文件中。javascript

nginx server 配置

文件位置: /etc/nginx/nginx.conf/default.conf 

server {
    # 端口號
    listen 80;
    # 域名 若是沒有域名填服務器ip地址
    server_name 47.102.195.218;

    index index.html index.htm;

    access_log /var/log/nginx/freedom01.access.log main;
    error_log /var/log/nginx/freedom01.error.log warn;

    location / {
        # 對應的靜態資源目錄
        root /data/www/test/freedom01/dist;
        index index.html index.htm;
        if (!-e $request_filename) {
            rewrite ^ /(.*) /index.html last;
            break;
        }
    }

    # 後臺接口地址以 /api 開頭
    location /api {
        limit_except GET POST {
            deny all;
        }
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        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_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        # 以 /api 開頭的請求轉發到本服務器3000端口/api
        proxy_pass http://127.0.0.1:3000/api;
        proxy_redirect off;
    }}
}複製代碼
相關文章
相關標籤/搜索