在我的生产环境中安装和配置 nginx

在我的生产环境中安装和配置 nginx

我知道有类似的问题,但我似乎无法解决我的情况。我正在安装和配置 nginx,以便为我的 VPS 提供有用且更快的服务器。我正在使用 Putty 命令提示符。我正在尝试为这个项目创建自己的结构。为什么 nginx.conf 语法不正确?为什么这个 nginx.conf 测试不成功是我无法理解的。有人可以提供一些反馈吗?

我收到此错误:

nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-enabled/default:18
nginx: configuration file /etc/nginx/nginx.conf test failed

配置详细信息如下:

# You may add here your
server {
        server_name 123.456.789.0;
        return 301 $scheme://example.com$request_uri;
# }
# statements for each of your virtual hosts to this file

### 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 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/RESTfulAPI;
        index index.php index.html index.htm;

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

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

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {

答案1

看起来您缺少了#前几行的一些注释()。

改变:

# You may add here your
server {
        server_name 123.456.789.0;
        return 301 $scheme://example.com$request_uri;
# }
# statements for each of your virtual hosts to this file

# You may add here your
# server {
#        server_name 123.456.789.0;
#        return 301 $scheme://example.com$request_uri;
# }
# statements for each of your virtual hosts to this file

答案2

引用您的错误:

nginx:[emerg] /etc/nginx/sites-enabled/default:18 中不允许使用“server”指令

该问题似乎位于server配置文件顶部的第一个指令处。

我发现有一个你评论过的右花括号:

# You may add here your
server {
        server_name 123.456.789.0;
        return 301 $scheme://example.com$request_uri;
# }
# statements for each of your virtual hosts to this file

server您可能想要完全注释以下块或删除花括号末尾的额外注释标签。

相关内容