如何设置nginx配置?

时间:2015-12-15 00:12:21

标签: node.js nginx admin backend

我一直在试图找出如何将服务器指向我的静态页面,我将其存储在我的云服务器的某个地方。另外,我使用NodeJ作为我的后端。

我的老问题:它没有指向我的myProjectX index.html而是指向Nginx默认网页"欢迎使用Nginx!"。我无法弄清楚原因; /​​

更新

我的新问题:在我执行了这个新配置之后,它指向了404页面,而不是指向我的index.html

另外,在第20行中,它给出了重复错误" /"当我运行sudo nginx -t进行调试时

`nginx: [emerg] duplicate location "/" in /etc/nginx/sites-enabled/default:20 `

这是我的Nginx配置文件......



server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name www.andii90s.com;

    root /home/andy/www/myProjectX/app;
    index index.html index.htm;


    location / {
        proxy_pass http://10.137.10.140:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location / {
        #Line 20
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
       root /home/andy/www/myProjectX/app;
    }
} 




2 个答案:

答案 0 :(得分:2)

首先,重启NGINX服务

service nginx restart

如果这不成功, 还要检查/ etc / nginx / sites-available /如果你使用的是默认值,如果你创建了一个新的,它必须在/ etc / nginx / sites-enabled /

中可用
sudo ln -s /etc/nginx/sites-available/yourconfig /etc/nginx/sites-enabled/

如果您想运营网站:

server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

root /home/andy/www/myProjectX/app;
index index.php index.html index.htm;

server_name myUrl;

location / {
    try_files $uri $uri/ =404;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
    root /home/andy/www/myProjectX/app;
}

并且,如果您想启用PHP,只需将其添加到您的配置文件中:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

如果你想使用NGINX作为代理,例如Node.js应用程序使用它:

server {
listen 80;
server_name example.com;

location / {
    proxy_pass http://hereip:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

这将使您的应用程序可以在您的根域中访问,例如:example.com。 ;)

答案 1 :(得分:2)

参考此博客 http://caffeinecodes.blogspot.in/2017/02/nginx-settings.html

server {
  listen 80;
  server_name test.com;
  location = /favicon.ico { access_log off; log_not_found off; }
  location /static/ {
      alias /Users/Afxal/workspace/test_project/static/;
  }
  location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://127.0.0.1:8000;
  }
}