Home Php C# Sql C C++ Javascript Python Java Go Android Git Linux Asp.net Django .net Node.js Ios Xcode Cocoa Iphone Mysql Tomcat Mongodb Bash Objective-c Scala Visual-studio Apache Elasticsearch Jar Eclipse Jquery Ruby-on-rails Ruby Rubygems Android-studio Spring Lua Sqlite Emacs Ubuntu Perl Docker Swift Amazon-web-services Svn Html Ajax Xml Java-ee Maven Intellij-idea Rvm Macos Unix Css Ipad Postgresql Css3 Json Windows-server Vue.js Typescript Oracle Hibernate Internet-explorer Github Tensorflow Laravel Symfony Redis Html5 Google-app-engine Nginx Firefox Sqlalchemy Lucene Erlang Flask Vim Solr Webview Facebook Zend-framework Virtualenv Nosql Ide Twitter Safari Flutter Bundle Phonegap Centos Sphinx Actionscript Tornado Register | Login | Edit Tags | New Questions | 繁体 | 简体


10 questions online user: 17

39
votes
answers
51 views
+10

Node.js + Nginx - What now?

I've set up Node.js and Nginx on my server. Now I want to use it, but, before I start there are 2 questions:

  1. How should they work together? How should I handle the requests?
  2. There are 2 concepts for a Node.js server, which one is better:

    a. Create a separate HTTP server for each website that needs it. Then load all JavaScript code at the start of the program, so the code is interpreted once.

    b. Create one single Node.js server which handles all Node.js requests. This reads the requested files and evals their contents. So the files are interpreted on each request, but the server logic is much simpler.

It's not clear for me how to use Node.js correctly.

沙发
+100

Node.js with Nginx configuration.

$ sudo nano /etc/nginx/sites-available/subdomain.your_domain.com

add the following configuration so that Nginx acting as a proxy redirect to port 3000 traffic from the server when we come from “subdomain.your_domain.com”

upstream subdomain.your_domain.com {
  server 127.0.0.1:3000;
}
server {
  listen 80;
  listen [::]:80;
  server_name subdomain.your_domain.com;
  access_log /var/log/nginx/subdomain.your_domain.access.log;
  error_log /var/log/nginx/subdomain.your_domain.error.log debug;
  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://subdomain.your_domain.com;
    proxy_redirect off;
  }
}
板凳
+80

answering your question 2:

I would use option b simply because it consumes much less resources. with option 'a', every client will cause the server to consume a lot of memory, loading all the files you need (even though i like php, this is one of the problems with it). With option 'b' you can load your libraries (reusable code) and share them among all client requests.

But be ware that if you have multiple cores you should tweak node.js to use all of them.

如果資源是您最重要的問題(不太可能),請遵循此建議。(a)和(b)之間存在不同的妥協。選項(a)可能更好,如果你希望站點更獨立,例如站點重啟或維護,數據庫連接,代碼庫,庫依賴關係,服務器之間的移動站點等。 - robocat 2016年2月8日在0:56

地板
+70

I made a repository in Github which you can clone, vagrant-node-nginx-boilerplate

basically the node.js app at /var/www/nodeapp is

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World
');
}).listen(4570, '127.0.0.1');

console.log('Node Server running at 127.0.0.1:4570/');

and the nginx config at /etc/nginx/sites-available/ is

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/nodeapp;
        index index.html index.htm;

        server_name localhost;

        location / {
          proxy_pass http://127.0.0.1:4570;
          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;
        }
}
4楼
+50

You could also use node.js to generate static files into a directory served by nginx. Of course, some dynamic parts of your site could be served by node, and some by nginx (static).

Having some of them served by nginx increases your performance..

5楼
+50

We can easily setup a Nodejs app by Nginx acting as a reverse proxy.
The following configuration assumes the NodeJS application is running on 127.0.0.1:8080,

  server{
     server_name domain.com sub.domain.com; # multiple domains

     location /{ 
      proxy_pass http://127.0.0.1:8080;  
      proxy_set_header Host $host;
      proxy_pass_request_headers on;  
     }

     location /static/{
       alias /absolute/path/to/static/files; # nginx will handle js/css
     }
   } 

in above setup your Nodejs app will,

  • get HTTP_HOST header where you can apply domain specific logic to serve the response. '
  • Your Application must be managed by a process manager like pm2 or supervisor for handling situations/reusing sockets or resources etc.

  • Setup an error reporting service for getting production errors like sentry or rollbar

NOTE: you can setup logic for handing domain specific request routes, create a middleware for expressjs application

使用pm2的另一個原因是,您可以在退出shell後“永遠”運行應用程序,並在需要重新啟動服務器時自動啟動它,請參閱:pm2.keymetrics.io/docs/usage/startup - SQUALLIE Dec 14 '18在0:04

過程經理 - 我是蝙蝠俠18年12月14日凌晨4點56分

@NickSteele:是的,僅適用於配置它可以正常工作。但JSON不僅用於配置。 - sampathsris於17年1月11日3:10

6楼
+30

Nginx can act as a reverse proxy server which works just like a project manager. When it gets a request it analyses it and forwards the request to upstream(project members) or handles itself. Nginx has two ways of handling a request based on how its configured.

  • serve the request
  • forward the request to another server

    server{
     server_name mydomain.com sub.mydomain.com;
    
     location /{ 
      proxy_pass http://127.0.0.1:8000;  
      proxy_set_header Host $host;
      proxy_pass_request_headers on;  
     }
    
     location /static/{
       alias /my/static/files/path;
     }
    

    }

Server the request

With this configuration, when the request url is mydomain.com/static/myjs.js it returns the myjs.js file in /my/static/files/path folder. When you configure nginx to serve static files, it handles the request itself.

forward the request to another server

When the request url is mydomain.com/dothis nginx will forwards the request to http://127.0.0.1:8000. The service which is running on the localhost 8000 port will receive the request and returns the response to nginx and nginx returns the response to the client.

When you run node.js server on the port 8000 nginx will forward the request to node.js. Write node.js logic and handle the request. That's it you have your nodejs server running behind the nginx server.

If you wish to run any other services other than nodejs just run another service like Django, flask, php on different ports and config it in nginx.

這只是重複的答案。請考慮刪除它; 你會保留積分。 - Dan Dascalescu於2014年11月25日6:59

這個答案有50個贊成票。根據1%的規則,可能有5000個用戶花時間閱讀這個答案,這對前一個答案沒有任何好處。它3歲的事實只會讓問題變得更糟:) - Dan Dascalescu 2014年11月28日21:00

@DanDascalescu - 如果您注意到,這兩個答案是在3年前的同一時間發布的。它們都提供相同的信息。這就是SO的情況,我不打算剔除我的一半答案,因為它們不是公認的答案。 - Mark Kahn 2014年11月28日22:25

我一個人發現這一系列的評論相當有趣,但答案本身就是浪費我的時間。...我不確定這是否意味著應該刪除答案,因為那樣我就不會看到評論主題。但除此之外,我會說是的。 - MalcolmOcean 2015年4月6日13:20

@DanDascalescu,我相信這個答案更明確,更直接。被接受的一個沒有提供一個用法示例,並且由於許多鏈接和額外的東西而令人困惑。 - andresgottlieb 2015年11月26日4:08

7楼
+10

You can run nodejs using pm2 if you want to manage each microservice means and run it. Node will be running in a port right just configure that port in nginx(/etc/nginx/sites-enabled/domain.com)

server{
    listen 80;
    server_name domain.com www.domain.com;

  location / {
     return 403;
  }
    location /url {
        proxy_pass http://localhost:51967/info;
    }
}

Check whether localhost is running or not by using ping.

And

Create one single Node.js server which handles all Node.js requests. This reads the requested files and evals their contents. So the files are interpreted on each request, but the server logic is much simpler.

This is best and as you said easier too

我喜歡這種方法不要求json文件是應用程序的本地文件。謝謝! - 查爾斯勃蘭特2014年5月8日16:52

0
votes
answers
55 views
+10

是否可以使用ipaddress限制訪問?

0

我最近安裝了SSL證書到網站。是否可以使用ipaddress限制訪問?

我試圖訪問使用IP地址

133.255.214.180 

它重定向現場

https://133.255.214.180/ 

則顯示,「您的連接不是私密連接」 SSL證書錯誤。我已經購買了域名而不是ip地址的ssl證書。

當訪問I使用域名,例如

www.example.com 

然後將其重定向到

https://www.example.com 

沒有SSL證書錯誤。

我使用重定向中的nginx作爲

server { 
    listen 80; 
    server_name 133.255.214.180; 

    return 301 https://www.example.com; 

} 

我想讓它這樣,當在瀏覽器的地址欄中的IP地址的用戶類型,然後將其重定向到域名即https://www.example.com使它不顯示ssl證書錯誤。這有可能實現嗎?

我很感激任何幫助。謝謝!

沙发
0
0

在發送實際請求之前完成HTTPS握手,因此如果輸入https://something_without_valid_cert,您將始終收到該錯誤。

隨着發送我指的是請求甚至「開口」 HTTP通信,包括髮送回重定向。

的原因很簡單,如果證書是無效的,你不能信任的服務器,它可以與任何迴應,包括惡意重定向。如果鍵入http://the_ip_address

你的nginx的配置應該工作。因爲您發回301(永久移動)瀏覽器下次將自動執行重定向,而無需調用服務器。

0
votes
answers
43 views
+10

Django需要Nginx幫助

1

我正在嘗試將我的網站部署到aws ec2。它在python/django中,我想學習如何自己部署網站。我對aws的EBS有一些問題,所以首先我想知道如何手動執行此操作。 我決定爲此使用gunicorn和nginx。Django需要Nginx幫助

我可以使用gunicorn在虛擬ENV運行的網站,我在/home/ec2-user/gunicorn_start.bash創建下面的腳本:

#!/bin/bash 

NAME="davidbiencom"         # Name of the 
application 
DJANGODIR=/home/ec2-user/davidbien    # Django project directory 
SOCKFILE=/home/ec2-user/virtual/run/gunicorn.sock 
USER=ec2-user          
GROUP=ec2-user          
NUM_WORKERS=3          
DJANGO_SETTINGS_MODULE=davidbiencom.settings    
DJANGO_WSGI_MODULE=davidbiencom.wsgi      

echo "Starting $NAME as `whoami`" 

# Activate the virtual environment 
cd $DJANGODIR 
source /home/ec2-user/virtual/bin/activate 
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE 
export PYTHONPATH=$DJANGODIR:$PYTHONPATH 
# Create the run directory if it doesn't exist 
RUNDIR=$(dirname $SOCKFILE) 
test -d $RUNDIR || mkdir -p $RUNDIR 

# Start your Django Unicorn 
# Programs meant to be run under supervisor should not daemonize themselves (do$ 
exec gunicorn ${DJANGO_WSGI_MODULE}:application  
--name $NAME  
--workers $NUM_WORKERS  
--user=$USER --group=$GROUP  
--bind=unix:$SOCKFILE  
--log-level=debug  
--log-file=- 

因爲沒有錯誤此運行良好,我相信。 接下來我安裝nginx並啓動服務。我確認它正在運行,因爲我收到歡迎頁面。接下來,我做到以下幾點:

  1. 轉到/etc/nginx/nginx.conf並添加以下到http

    包括/etc/nginx/sites-enabled/*.conf;

然後我在/ etc/nginx/sites-available和sites-enabled中創建了兩個文件夾。 我創建一個文件davidbien.conf並輸入以下內(修訂版):

upstream app_server { 
# fail_timeout=0 means we always retry an upstream even if it failed 
# to return a good HTTP response 

# for UNIX domain socket setups 
server unix:/home/ec2-user/virtual/run/gunicorn.sock fail_timeout=0; 

# for a TCP configuration 
# server 192.168.0.7:8000 fail_timeout=0; 
} 

server { 
listen 80; 
server_name 35.176.185.50; 

#Max upload size 
client_max_body_size 75M; # adjust to taste 

location /static/ { 
    root /home/ec2-user/davidbien/static; 
} 

location/{ 
    # checks for static file, if not found proxy to app 
    try_files $uri @proxy_to_app; 
} 

location @proxy_to_app { 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    # enable this if and only if you use HTTPS 
    # proxy_set_header X-Forwarded-Proto https; 
    proxy_set_header Host $http_host; 
    # we don't want nginx trying to do something clever with 
    # redirects, we set the Host: header above already. 
    proxy_redirect off; 
    proxy_pass http://app_server; 
    } 
} 

我保存這個文件並運行以下命令:

ln -s /etc/nginx/sites-available/davidbiencom.conf /etc/nginx/sites-enabled/davidbiencom.conf 

此步驟完成後我重啓nginx的,我當我輸入IP地址時,我得到502錯誤的網關錯誤。

這裏有什麼問題? 謝謝。

編輯: 這裏的錯誤日誌的無功/日誌/ nginx的/ error.log中

2017/11/10 22:26:27 [error] 27620#0: *1 open() "/usr/share/nginx/html/favicon.iicon.ico" failed (2: No such file or directory), client: 2.96.149.96, server: $localhost, request: "GET /favicon.ico HTTP/1.1", host: "35.176.185.50",referrer: "http://35.176.185.50/" 

編輯2: 這裏的等/ nginxnginx/conf目錄文件:

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; 

# Load modular configuration files from the /etc/nginx/conf.d directory. 
# See http://nginx.org/en/docs/ngx_core_module.html#include 
# for more information. 
include /etc/nginx/sites-enabled/*.conf; 

index index.html index.htm; 

server { 
    listen  80 default_server; 
    listen  [::]:80 default_server; 
    server_name localhost; 
    root   /usr/share/nginx/html; 

    # Load configuration files for the default server block. 
    include /etc/nginx/default.d/*.conf; 

    location/{ 

# redirect server error pages to the static page /40x.html 
    # 
    error_page 404 /404.html; 
     location = /40x.html { 
    } 

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

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80 
    # 
    #location ~ .php$ { 
    # proxy_pass http://127.0.0.1; 
    #} 
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
    # 
    #location ~ .php$ { 
    # root   html; 
    # fastcgi_pass 127.0.0.1:9000; 
    # fastcgi_index index.php; 
    # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 
    # include  fastcgi_params; 
    #} 
# deny access to .htaccess files, if Apache's document root 
    # concurs with nginx's one 
    # 
    #location ~ /.ht { 
    # deny all; 
    #} 
} 
沙发
0
1

你」重新告訴nginx它必須將請求轉發到127.0.0.1:3031,但是從我從腳本看到的啓動gunicorn的內容中,gunicorn被綁定到一個套接字上,如果你將在127.0.0.1:3031上啓動你的gunicorn worker,它應該工作

+0

現在我想起來了,我不需要那部分,是嗎?你認爲它可以被刪除? 刪除該行後,我得到403禁止。 – davidb

+0

我認爲gunicorn將在端口8000上啓動,如果你刪除它 –

+0

但不是nginx應該將所有來自端口80的流量都指向該應用程序嗎? 我應該讓它0.0.0.0:80然後呢?我看到一些教程沒有這個設置。 – davidb

0
votes
answers
52 views
+10

HTTPS到HTTP重定向nginx

0

問題是,我的本地nginx服務器上的ssl證書已過期,我需要幾天的時間才能獲得新的證書。 該網站通過http工作,但是,瀏覽器記住https的現金(並且有很多本地用戶)。HTTPS到HTTP重定向nginx

要解決這個問題,我必須強制將https重定向到http。我生成的自簽名證書,並添加幾行代碼:

server { 
    listen 443; 
    server_name site.com; 
    ssl on; 
    rewrite^http://$server_name$request_uri? permanent; 

    ssl_certificate /etc/ssl/site/cert.crt; 
    ssl_certificate_key /etc/ssl/site/sert.key; 
} 

server { 
    listen 80; 
    ssl off; 
    .... 
} 

通過https該網頁提供了隱私錯誤的Net :: ERR_CERT_AUTHORITY_INVALID

我知道,通過443端口瀏覽器嘗試設置安全頻道先發送http請求,所以我需要某種解決方法。

任何人都可以幫忙嗎?

沙发
0
0

這個問題已經answered

但是here你可以得到一個免費的,被廣泛接受的證書!

+0

這個答案對我來說是不可接受的,因爲我的證書是自簽名的並給出錯誤。我無法獲得免費的證書,因爲我的服務器是本地的(不通過www接受) – Leeloo

0
votes
answers
51 views
+10

NodeJS oAuth Google Nginx代理

0

我在nginx代理下的oAuth重定向時遇到問題。我在我的網站上有Google社交登錄按鈕,並且在本地運行良好。當我在線上傳我的項目時,我把它放在nginx代理下,它不再工作。當我點擊登錄時,瀏覽器將我重定向到localhost:3000/auth/google/callback而不是mywebpage.com/auth/google/callback,告訴我該頁面不存在。
我知道這個問題:Nginx proxy with Google OAuth 2.0和我試圖使用他們建議的修復程序,但他們沒有工作,因爲我不運行流星,但與Nginx代理Express上的NodeJS。
他們建議的第二種解決方案是使用proxy_redirect,但它似乎不起作用。我有一個與谷歌oAuth,但每次我點擊它(這是一個瀏覽器重定向,而不是一個彈出)它將我重定向到本地主機:3000這是代理服務器後面的網址。
我嘗試了proxy_redirect的所有可能的配置,但它總是將我重定向到那裏。這是我在nginx中的代理部分:NodeJS oAuth Google Nginx代理

location/{ 
    proxy_pass http://127.0.0.1:3000; 
    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; 
    proxy_redirect http://localhost:3000 https://mywebsite.com; 
} 

任何建議嗎?

沙发
0
0

你把這裏面的服務器塊?

server { 
    listen 80; 
    server_name example.com; 
    location/{ 
     proxy_pass http://127.0.0.1:3000; 
     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; 
    } 
} 

嘗試刪除proxy_redirect。我不認爲你可以代理從http到https或https到http。據我所知,協議需要保持不變。

4
votes
answers
44 views
+10

Too many open files when using Memcached for sessions

I'm using ubuntu + nginx + php5-fpm + symfony2, and I recently switched from files to Memcached (php5-memcached) for sessions. Since then, I've started to get 500 response code errors after a while. This is what it looks like in the error logs (failed to open stream: Too many open):

"PHP message: PHP Warning:  simplexml_load_file(.../vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config/doctrine/User.orm.xml): failed to open stream: Too many open files in .../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php on line 736
PHP message: PHP Warning:  simplexml_load_file(): I/O warning : failed to load external entity ".../vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config/doctrine/User.orm.xml" in .../vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php on line 736
PHP message: PHP Warning:  include(.../vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php): failed to open stream: Too many open files in .../vendor/composer/ClassLoader.php on line 382

After some research, I've found out that it seems to be related to the usage of Memcached for sessions:

  1. Those are the php5-fpm processes running:

    ps aux | grep php

    root 3233 0.0 0.2 207464 16164 ? Ss Mar16 0:02 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
    www-data 3236 0.0 0.8 238796 65796 ? S Mar16 0:50 php-fpm: pool www
    www-data 3646 0.0 0.7 231176 57808 ? S Mar16 0:42 php-fpm: pool www
    www-data 7503 0.1 0.7 234820 59920 ? S 11:52 0:34 php-fpm: pool www
    ubuntu 8224 0.0 0.0 10436 860 pts/0 S+ 16:53 0:00 grep --color=auto php

2.Checking any of the php5-fpm childs with lsof, it shows, among other things that there were 1k+ open file descriptors pointing to the Memcache instance with an established connection:

sudo lsof -p 3236

…
php5-fpm 3236 www-data  672u  IPv4             107781      0t0    TCP hostname:54403->memcacheIp:11211 (ESTABLISHED)
php5-fpm 3236 www-data  673u  IPv4             108827      0t0    TCP hostname:54411->memcacheIp:11211 (ESTABLISHED)
php5-fpm 3236 www-data  674u  IPv4             107800      0t0    TCP hostname:54418->memcacheIp:11211 (ESTABLISHED)
... 

Over one thousand entries like that, which I think starts failing when it arrives to 1024, which is the limit of open file descriptors:

ulimit  -n
1024

The thing is that this is a QA environment which is not used for more than 1 or 2 concurrent users, so there is no way all those open file descriptors are active sessions. I noticed that every time I make a request to the symfony2 application, a new socket connection to the memcache server is opened, but then it's never closed. So, eventually it reaches the limit at starts failing. I thought might be something related with timeout connections or something like that but haven't found anything so far.

Restarting php5-fpm seems to "solve" the problem, but only until another 1k requests are made.

I've temporarily switched back to files and the problem disappears. Also I've tried using Memcache (php5-memcache) instead of Memcached and the problem is solved but I'd rather use php5-memcached as it seems better maintained.

Any ideas of how to solve this problem?

Thank you very much!

If it helps, the specific php version that I'm using:

PHP 5.5.9-1ubuntu4.6 (cli) (built: Feb 13 2015 19:17:11) 
libmemcached version => 1.0.8
沙发
+40
+50

好的,我已經找到了解決方案,感謝:

http://php.net/manual/en/memcached.construct.php(參見@Tobias評論)

這個https://gist.github.com/K-Phoen/4327229(參見@cmenning評論)

我使用Memcached和sessions.yml如下:

session.memcached:
    class: Memcached
    arguments:
       persistent_id: %session_memcached_prefix%
    calls:
        - [ addServer, [ %session_memcached_host%, %session_memcached_port% ]]

session.handler.memcached:
    class:     SymfonyComponentHttpFoundationSessionStorageHandlerMemcachedSessionHandler
    arguments: [@session.memcached, { prefix: %session_memcached_prefix%, expiretime: %session_memcached_expire% }]

事實證明,如果為Memcached提供persistent_id參數,它將保持請求之間的連接。來自php.net:

persistent_id

默認情況下,Memcached實例在請求結束時被銷毀。要創建在請求之間保持不變的實例,請使用persistent_id為實例指定唯一ID。使用相同的persistent_id創建的所有實例將共享相同的連接。

問題是如果你使用persistent_id(性能更好),你必須檢查在調用之前是否已經添加了服務器addServer,否則它會添加一個新服務器並創建一個新連接,即使它是同一個服務器(它沒有檢查欺騙。這樣的事情:

        $instance = new Memcached($persistent_id);

        // Add server if no connections listed. 
        if (!count($instance->getServerList())) {
            $instance->addServers($server);
        }

但是為此你需要創建一個Memcached的包裝類。簡單的解決方法就是註釋掉這一行,並避免暫時使用持久連接:

    #arguments:
    #  persistent_id: %session_memcached_prefix%

這解決了這個問題,即使它並不理想。我希望它可以幫助您節省一些時間,避免頭痛!謝謝大家的幫助。

0

我們遇到了這個問題,我們通過在config.yml中的memcache中緩存元數據來解決這個問題

 orm:

    default_entity_manager:   default
    entity_managers:
      default:
         metadata_cache_driver:
             type: memcache
             host: localhost
             port: 11211
             instance_class: Memcache
         query_cache_driver:
             type: memcache
             host: localhost
             port: 11211
             instance_class: Memcache

否則,如果你喜歡通過apc緩存它也修復它,我嘗試了兩個:

 metadata_cache_driver: apc
 query_cache_driver: apc

希望這有幫助,有關此問題的更多信息:https//github.com/FriendsOfSymfony/FOSUserBundle/issues/1062#issuecomment-29473883

謝謝,但我不認為這是同一個問題,因為在我的情況下,它無法打開文件,因為系統已達到最大打開文件描述符的限制,而該錯誤似乎是解析文件的問題。 - 哈維爾CH 2015年3月18日10:02

啊,我的壞,我沒有註意到“未能打開流:太多打開文件”,因為我最近痴迷於該錯誤..我假設重新啟動內存緩存並清除緩存會暫時修復它? - Nawfal Serrar 2015年3月18日10:08

重新啟動php5-fpm臨時修復它(可能還會重啟memcache),但是你知道,只是暫時的!呵呵。我需要一個合適的解決方案......無論如何,謝謝你的幫助。 - 哈維爾CH 2015年3月18日10:10

順便說一句,如果你在memcache或apc中使用元數據緩存,它不會繼續從它執行的文件加載xml數據,所以我仍然認為它可以解決它 - Nawfal Serrar 2015年3月18日在10:10

我試一試以防萬一,但問題仍然存在。唯一的區別是現在無法嘗試打開另一個文件:PHP消息:PHP警告:include(... / vendor / symfony / symfony / src / Symfony / Component / Debug / Exception / FatalErrorException.php):無法打開stream:第382行的... / vendor / composer / ClassLoader.php中有太多打開的文件但是它的錯誤相同。 - 哈維爾CH 2015年3月18日10:47

0
votes
answers
45 views
+10

如何更改與docker一起使用時的nginx端口

0

我是碼頭初學者,我做的第一件事是下載nginx並試圖將其掛載到80:80端口,但Apache已經坐在那裏。如何更改與docker一起使用時的nginx端口

docker container run --publish 80:80 nginx 

docker container run --publish 3000:3000 nginx

我試圖做這樣的3000:3000使用它在端口3000,但它不能正常工作。而且它不記錄任何東西,我可以使用全球化志願服務青年。

沙发
0
5

當您開始使用Docker時,您可能會在DockerHub上找到有關圖像的有用信息。例如用nginx你有一段關於如何去expose public ports

你可以使用:在你的本地主機

docker run --publish 3000:80 nginx 

3000端口將被轉發到端口80是nginx的圖像使用等待HTTP連接的端口。

我也推薦你閱讀這個官方文檔about networking in Docker

0
votes
answers
55 views
+10

在Nginx服務器中設置HMVC Codeigniter

0

在Nginx服務器上設置Codeigniter HMVC。我搜索了很多,但沒有得到完美的解決方案。 任何人都有演示請分享。在Nginx服務器中設置HMVC Codeigniter

+3

你嘗試了什麼?只需使用https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/和HMVC第三方,無需做更多 –

沙发
0
0

是的,我試過了,但沒有重寫規則HMVC使其工作。

0
votes
answers
47 views
+10

NGINX + Docker Compose路由問題

0

我有一個docker撰寫文件,包含兩個微服務,我想利用URI路由來處理CORS。這裏是我撰寫的文件:NGINX + Docker Compose路由問題

version: "3.1" 
services: 
    auth-api: 
    image: xxxx/auth-api:latest 
    restart: always 
    depends_on: 
     - "user-api" 
    user-api: 
    image: xxxx/user-api:latest 
    restart: always 
nginx: 
    image: nginx 
    restart: always 
    ports: 
     - "80:80" 
    links: 
     - "auth-api" 
     - "user-api" 
    volumes: 
     - ./nginx:/etc/nginx 

我想用NGINX的路由,但我遇到了問題,用404的或者405試圖訪問該資源時的。我嘗試了幾種不同的配置,有一段時間NGINX說它在開始時找不到api端點,我認爲我的問題主要是圍繞路由配置。對於這些請求,我希望它是/auth/user

worker_processes 1; 

events { worker_connections 1024; } 

http { 

    sendfile on; 

    upstream auth-target { 
     server auth-api:8080; 
    } 

    upstream user-target { 
     server user-api:8080; 
    } 

    server { 
     listen  80; 

     location /auth { 

      proxy_pass http://auth-target; 

      proxy_redirect  off; 
      proxy_set_header Host $host; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header X-Forwarded-Host $server_name; 
     } 

     location /user { 

      proxy_pass http://user-target; 

      proxy_redirect  off; 
      proxy_set_header Host $host; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header X-Forwarded-Host $server_name; 
     } 


    } 

} 

我對NGINX很新,所以我不確定自己做的是對的。我還應該注意到,這兩個API都有8080作爲容器構建的一部分公開。

沙发
0
0

所以這是我的問題。路由工作正常,但我從我的服務中返回404。這是因爲位置路由與請求一起傳遞。所以,最後我發現了這樣的回答:

https://serverfault.com/questions/562756/how-to-remove-the-path-with-an-nginx-proxy-pass

它指向你需要一個尾隨斜線,以去除的位置,所以我的配置文件最終看上去是這樣的:

location /auth/ { 

      proxy_pass http://auth-target/; 

      proxy_redirect  off; 
      proxy_set_header Host $host; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header X-Forwarded-Host $server_name; 
     } 
90
votes
answers
35 views
+10

How to verify if nginx is running or not?

After running an ASP.NET vNext project on my local machine I was trying to figure out how I can run it on nginx as it looks to be a recommended choice

Following jsinh's blog, I installed it using:

sudo apt-get update
sudo apt-get install nginx -y

I was trying to understand whether it is working or not by using:

ifconfig eth0 | grep inet | awk '{ print $2}'

After running

sudo service nginx start
sudo service nginx stop

However, the output is always the same:

Nginx status

How to verify if nginx is running or not?

up vote 56 down vote accepted favorite
沙发
+560
+50

根據您的要求,以下命令應幫助

service nginx status

如果服務不可用怎麼辦? - fraxture 18年9月30日11:01

如果服務不可用,那麼可以使用這個:sudo /etc/init.d/nginx status - Biranchi 1月9日6:50

+150

這可能是系統依賴的,但這是我找到的最簡單的方法。

  if [-e /var/run/nginx.pid]; 然後迴聲“nginx正在運行”; fi   

這是腳本編寫的最佳解決方案。

如果nginx突然死亡,這不會失敗嗎? - Mehrdad 4月22日20:20

+110

您可以使用 lsof 查看端口80上正在偵聽的應用程序:

  sudo lsof -i TCP:80  
     
			
        

sudo lsof -i是給我的 - joey 17年7月8日在22:00

+30

現代( systemctl )方式:

  systemctl is-active nginx   

你可以使用shell腳本中的退出值如下:

  systemctl -q is-active nginx&& echo“它是活躍的,做某事” 
     
			
        
+20
<p>上述答案都不適合我,所以讓我分享一下我的經驗。我在一個docker容器中運行nginx,它有一個端口映射(hostPort:containerPort) - 80:80上面的答案給了我奇怪的控制台輸出。只有好的舊'nmap'才能完美無缺,甚至可以捕獲nginx版本。為我工作的命令是:</ p> <p> <strong> nmap -sV localhost -p 80 </ strong> </ p> <p>我們正在使用localhost和port上的-ServiceVersion開關執行nmap: 80。對我很有用。</ p>
+20

還可以使用以下代碼檢查nginx狀態:

  sudo /etc/init.d/nginx status  
     
			
        
+10
<p>不確定您關注哪個指南,但如果您查看此頁面,</ p> <p> <a href =“https://www.digitalocean.com/community/tutorials/how-to-install -nginx-on-ubuntu-14-04-lts“rel =”nofollow“> https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-14-04-lts </a> </ p> <p>它使用另一個命令</ p> <pre> <code> ip addr show eth0 | grep inet | awk'{print $ 2; }'| sed的//.*.//'</ code> </ pre> <p>並且還指出了預期的結果。</ p>

@student這個帖子對於像ASP.NET Core這樣的移動目標而言太舊了,它錯過了很多細節。 - Lex Li 2016年2月5日10:23

很好,至少它給了我一個起點。如果你能展示更新的替代品? - 學生於2016年2月5日15:54