如何让 nginx 在 HTTP 上提供服务

如何让 nginx 在 HTTP 上提供服务

我有一个 nginx 实例,它在 HTTPS 上可以正常运行,但在 HTTP 上却不行。

当我去http://myserver.com/something然后它只是提供,但是,如果我去https://myserver.com/something然后它提供页面。

这就是我的路径寻找的方式sites-available

root@myserver:~# ls -al /etc/nginx/sites-available/
total 28
drwxr-xr-x 3 root root 4096 May 11 15:37 .
drwxr-xr-x 7 root root 4096 Feb 11 22:46 ..
-rw-r--r-- 1 root root  586 Oct  6  2015 default
-rw-r--r-- 1 root root 1901 Oct  6  2015 default.orig
drwxr-xr-x 2 root root 4096 May 11 15:30 locations
-rw-r--r-- 1 root root 1460 May 11 15:37 ssl_proxy

这是我的道路sites-enabled

root@myserver:~# ls -al /etc/nginx/sites-enabled/
total 8
drwxr-xr-x 2 root root 4096 May 11 15:32 .
drwxr-xr-x 7 root root 4096 Feb 11 22:46 ..
lrwxrwxrwx 1 root root   28 Jun 29  2016 ssl_proxy -> ../sites-available/ssl_proxy

我的ssl_proxy样子是这样的:

server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;

    ssl_certificate /etc/nginx/ssl/cert.pem;
    ssl_certificate_key /etc/nginx/ssl/key.pem;
    ssl_dhparam /etc/nginx/ssl/dhparam.pem;
    ssl_trusted_certificate /etc/nginx/ssl/ca.pem;

    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;


    # modern configuration. tweak to your needs.
    ssl_protocols TLSv1.2;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
    ssl_prefer_server_ciphers on;

    # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
    add_header Strict-Transport-Security max-age=15768000;

    # OCSP Stapling ---
    # fetch OCSP records from URL in ssl_certificate and cache them
    ssl_stapling on;
    ssl_stapling_verify on;


    resolver <ipaddress_here>;

    include /etc/nginx/sites-available/locations/*.conf;
}

问题

我怎样才能得到http同样的服务?

答案1

您不应该同时在两种协议上提供内容,而应该从 http 转发到 https

# Forward non-www requests to www
server {
  listen       80;
  server_name  example.com www.example.com;
  return       301 https://www.example.com$request_uri;
}

如果你确实想在上面的服务器上运行 http 服务,你需要告诉 Nginx 监听 80 端口。配置将像这样开始

server {
  listen 443 ssl default_server;
  listen [::]:443 ssl default_server;
  listen       80;  # NEW LINE
  # etc
}

这是微不足道的 Nginx。在你问类似问题之前,我建议你自己做一些研究和学习。

相关内容