首页 > 解决方案 > 基本的 Nginx 配置

问题描述

背景,我一直在像这样在 https 上使用 Node/expressJS

const httpsOptions = {
  key: fs.readFileSync('config/example_com.key'),
  cert: fs.readFileSync('config/example_com.crt'),
  ca: [
      fs.readFileSync('config/COMODORSAAddTrustCA.crt'),
      fs.readFileSync('config/COMODORSADomainValidationSecureServerCA.crt')
  ]
};
https.createServer(httpsOptions, app).listen(port, function () {
});

一切正常 - https 建立良好。现在我的目标是建立 i) 帮助服务器负载;ii) 如果流量过多(而不是让网站崩溃),则路由到一个静态页面,上面写着“现在人太多,请稍后再试”。

为此,我只是试图迁移到 nginx 作为同一服务器上的反向代理和相同的所有内容,但它根本不会做!

我最近尝试过,当我去 https://www.example.com/images/picture.jpg 时,它是“403 Forbidden”


任何人都可以帮助校对我当前的设置吗?



user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

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;
    include /etc/nginx/conf.d/*.conf;


    server {
        listen       443 ssl default_server;
        listen       [::]:443 default_server;
        server_name  example.com www.example.com;

        ssl on;
        ssl_certificate  /home/ec2-user/MIP/config/example_com.crt; 
        ssl_certificate_key /home/ec2-user/MIP/config/example_com.key;

        if ($http_host = www.example) {
          return 302 https://example.com$request_uri;
        }
        root         /home/ec2-user/MIP/views;

        include /etc/nginx/default.d/*.conf;
        location / {
          return 302 https://www.example.com$request_uri;
        }
        location /images/ {
          root /home/ec2-user/MIP/public;
        }
    }
}

是的,我在 /home/ec2-user/MIP/public/images/picture.jpg 确实有一个 .jpg 文件,并且我已经在这个文件上完成了 chmod +r 。

另外,为了记录,现在,我有 sudo killall 节点,否则当我执行“sudo service nginx start”时,它会抱怨“nginx.service 的工作失败,因为控制进程以错误代码退出。请参阅“systemctl status nginx.service”和“journalctl -xe”了解详情。”

提前谢谢了

[更新] 作为记录,我实际上已经通过以下方式获得了到外部链接位的路由:

location /images/ {
          return 302 https://<bucketName>.s3-us-west-2.amazonaws.com/public$request_uri;
}

现在只为从绝对路径提供内部内容而苦苦挣扎 - 尽管我已经在公用文件夹上完成了 chown -R 并且也在文件上完成了 chmod +r ,但它一直给我 403 Forbidden

location /js/ {
          root /home/ec2-user/MIP/public/;
}

标签: sslnginxhttpsinstallation

解决方案


推荐阅读