nginx 配置文件中的语法错误

nginx 配置文件中的语法错误

我正在尝试在运行 Raspbian 的 Raspberry Pi B+ 上配置 nginx。

下列的本指南由 Pestmeester 编译和改编 我设法在端口 80 上配置了 http 服务器,并且网站已启动并运行。我向 sites-available 添加了文件,但我的服务器似乎有问题,并抱怨:

$ 重新启动 nginx:nginx:[emerg] /etc/nginx/sites-enabled/mysite.com:147 中不允许使用“server”指令 $ nginx:配置文件 /etc/nginx/nginx.conf 测试失败

这是我的/etc/nginx/sites-available/mysite.com文件的内容:

# You may add here your
# server {
#       ...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
        #listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

        root /data/www;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name mysite.com.local mysite.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                allow ::1;
                deny all;
        }


        location ~ [^/].php(/|$) {
                fastcgi_split_path_info ^(.+?.php)(/.*)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
        #location /RequestDenied {
        #       proxy_pass http://127.0.0.1:8080;
        #}
        error_log /data/logs/error.log error;
        #error_page 404 /404.html;
        access_log /data/logs/access.log;

        # redirect server error pages to the static page /50x.html
        #
        #error_page 500 502 503 504 /50x.html;
        #location = /50x.html {
        #       root /usr/share/nginx/www;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #       fastcgi_pass unix:/var/run/php5-fpm.sock;
        #       fastcgi_index index.php;
        #       include fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#       listen 8000;
#       listen somename:8080;
#       server_name somename alias another.alias;
#       root html;
#       index index.html index.htm;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

# HTTPS server
#
server {
        listen 443 ssl;
        server_name mysite.com www.mysite.com;

        ssl_certificate          /etc/nginx/ssl/cert_chain.crt;
        ssl_certificate_key      /etc/nginx/ssl/mysite.com.key;

        #enables all versions of TLS, but not SSLv2 or 3 which are weak and now deprecated.
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

        #Disables all weak ciphers
        ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA$

        ssl_prefer_server_ciphers on;

        root /data/www;
        index index.php index.html index.htm;

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /data/www;
        }

        # Error & Access logs
        error_log /data/logs/error.log error;
        access_log /data/logs/access.log;

        location / {
                index index.html index.php;
        }

        location ~ \.php(?:$|/) {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
                fastcgi_param HTTPS on;
                server unix:/var/run/php5-fpm.sock;
        }
}

我也查看了之前有类似错误警告的帖子,大多数都是由于缩进不正确或只是在行尾省略了 ; 而导致的。我试图小心谨慎,不再犯这样的错误,但我搞不清楚问题出在哪里。如果能提供任何关于如何解决这个问题的建议,我将不胜感激。

答案1

根据@AD7six的建议,我将 nginx 配置文件的最后一行从 更改为server unix:/var/run/php5-fpm.sock;fastcgi_pass unix:/var/run/php5-fpm.sock;现在我的网站已通过 https 启动并运行。

答案2

这是我添加的部分

您是否像这样将其添加到现有的第一个服务器块中?从错误中可以看出您这样做了:

server {
    ... http stuff

    server {
        ...https stuff
    }
}

这不是正确的语法,服务器块必须是独立的。

server {
    ... http stuff
}
server {
    ...https stuff
}

您没有包含最终文件,因此很难确定,但至少这就是该错误的含义。

答案3

假设您希望整个网站运行 SSL,则必须将 http 流量 301 转为 https,如下所示:

server {
       listen         80;
       server_name    www.example.com example.com;
       return         301 https://www.example.com$request_uri;
}

server {
       listen         443 ssl;
       server_name    www.example.com;

       [... SSL Server Stuff ...]
}

这应该捕获所有带有或不带有 www 的 http 请求,并将所有请求重定向到 SSL。

相关内容