Nginx 配置

684 阅读1分钟

安装 nginx

# 更新软件包
$ yum -y update

# 安装nginx
$ yum install nginx

# 检查nginx
$ nginx -v

nginx 相关文件位置

/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志

常用命令

nginx -t                  # 查看nginx状态
nginx -s reload           # 重新载入配置文件
nginx -s reopen           # 重启 Nginx
nginx -s stop             # 停止 Nginx

nginx 配置文件

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

初始 nginx.conf 文件内容为 demo 内容,根据情况修改。

#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/conf.d/test.conf 文件中,test.conf 是你新建的 server 配置文件(名称看着取)。

引入单独的 server 配置文件:include /etc/nginx/conf.d/*.conf 

nginx server 配置

文件位置:/etc/nginx/conf.d/test.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;
    }}
}

常见报错

1. 执行 nginx -s reload 报错 nginx: [error] invalid PID number

解决方法:执行 nginx -c /etc/nginx/nginx.conf

如果还没解决,参考 blog.csdn.net/PT1993/arti…

2. nginx 出现 403 Forbidden

解决方案:

方案1:nginx.conf 文件中的 user 和启动用户不一致,设置为一致即可;

方案2:nginx 没有 web 目录的操作权限,修改 web 目录的读写权限,重启 nginx 即可;

chmod -rw 777