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: 24

0
votes
answers
27 views
+10

Docker-compose + Nginx SSL反向代理

0

我試圖使用Nginx設置反向代理,它也會爲後端服務提供https。Docker-compose + Nginx SSL反向代理

我有3個容器,一個用於mongodb,一個用於我的.NET核心後端應用程序和一個用於反向代理。

碼頭集裝箱似乎運作良好,直到香港專業教育學院設置HTTPS它運作良好。

問題是,來自https://localhost:8080的請求未正確轉換爲在http端口上運行的.NET核心應用程序。

問題出在我的Nginx配置文件中,但我不知道如何解決它。

worker_processes 1; 

events { worker_connections 1024; } 

http { 

    sendfile on; 

    upstream web { 
     server web:443; 
    } 

    server { 
     listen 8080; 

     location /upstream { 
      proxy_pass   https://web; 
      proxy_ssl_certificate  /etc/nginx/cert.pem; 
      proxy_ssl_certificate_key /etc/nginx/privkey.pem; 
      proxy_ssl_session_reuse on; 
      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; 
     } 
    } 

    server { 
     listen 443 ssl; 

     ssl_certificate_key /etc/nginx/privkey.pem; 
     ssl_client_certificate /etc/nginx/cert.pem; 
     ssl_verify_client  off; 

     location/{ 
      proxy_pass http://web; 
     } 
    } 
} 

當我這樣做的HTTP請求,生病用https://localhost:8080時,它會返回ERR_SSL_PROTOCOL_ERROR拿回502網關錯誤。

在終端中,nginx的容器返回

enter image description here

任何想法?

+0

你是什麼意思'沒有正確翻譯?哦 - 你可能會嘗試添加'X-Forwarded-Proto',你的應用程序需要相信它..也許相關:https://forums.iis.net/t/1178094.aspx?Rewriting+HTTPS+variable+based+ on + value + of + X + Forwarded + Proto – gusto2

+0

如果有幫助,你可以試試這個:https://github.com/evertramos/docker-compose-letsencrypt-nginx-proxy-companion – Evis

沙发
0
0

閱讀了一下後,文檔我找到了解決方案。

worker_processes 1; 

events { worker_connections 1024; } 

http { 

    sendfile on; 

    upstream web { 
     server web:80; 
    } 

    server { 
     listen 8080 ssl; 

     ssl_certificate   /etc/nginx/cert.pem; 
     ssl_certificate_key  /etc/nginx/privkey.pem; 

     location/{ 
      proxy_pass   http://web; 
      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; 
      proxy_set_header X-NginX-Proxy true; 
     } 
    } 
} 
0
votes
answers
43 views
+10

使用Robo 3T連接到遠程MongoDB

1

我試圖在Mac下使用Robomongo(或Robo 3T)來控制遠程Ubuntu & Nginx服務器中的我的mongodb。使用Robo 3T連接到遠程MongoDB

通常情況下,我需要在終端上使用ssh xxx.xx.xx.xx,並使用用戶名和密碼連接到服務器。在/etc/nginx/sites-enabled/myweb.io,有listen 443 ssl

在Robo 3T中,我試圖建立與Use SSH tunnel的連接。我嘗試了端口號44380。但它給了我一個錯誤:Error: Resource temporarily unavailable. Error when starting up SSH session: -13. (Error #35)

有誰知道如何解決這個問題?

enter image description here

+0

嘗試連接而不SSH隧道和SSH在端口22上運行(如果你沒有改變它,但你應該改變它)。 – Shubhamoy

+0

如果我沒有SSH隧道連接,我應該使用哪種方式? – SoftTimur

+0

只需取消SSH隧道並嘗試連接。 – Shubhamoy

沙发
0
1

正確的設置是

SSH

1)下,檢查User SSH tunnel,使用端口22

2)和下Connection,寫127.0.0.1:27017

0
votes
answers
34 views
+10

爲什麼在NGINX中重寫這個不起作用?

-1

試圖在本地服務器上添加重寫,但我卡住了。爲什麼在NGINX中重寫這個不起作用?

這就是我所擁有的。

location/{ 
     # First attempt to serve request as file, then 
     # as directory, then fall back to displaying a 404. 
     # try_files $uri $uri/ =404; 
     try_files $uri $uri/ $uri.html $uri.php?$query_string; 
    } 

    location /news/post/ { 
     rewrite ^/news/post/([a-zA-Z0-9-])?$ /news/post.php?id=$1 break; 
    } 

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
    # 
    location ~ .php$ { 
     include snippets/fastcgi-php.conf; 
     #fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
     fastcgi_pass 127.0.0.1:9000; 
     #extra 
     fastcgi_split_path_info ^(.+.php)(/.+)$; 
     #--extra 
    } 

當去/新聞/職位/它說:「您已選擇打開此文件」,並問我是否要下載一個BIN文件。

如果我去/ news/post /這是一個新聞標題,我會得到一個404 Not Found。

我在做什麼錯?

+0

堆棧溢出:爲什麼有人會投下一個問題?那有什麼意義呢? –

沙发
0
0

工程。改變了一下,需要完整的路徑。

location /news/post/ { 
     rewrite ^/news/post/(.*)$ http://www.myserver.com/news/post.php?id=$1 break; 
    } 
0
votes
answers
21 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
14 views
+10

nginx Ubuntu nodeJs - 服務器只處理來自本地網絡的請求

0

我整天跟隨各種指令來啓動並運行第一臺服務器。我在3臺設備上測試過它,並認爲我擁有它......直到我的朋友嘗試它並發現「網站無法訪問」。nginx Ubuntu nodeJs - 服務器只處理來自本地網絡的請求

在重複檢查我的結果時,我發現如果我將手機與Wi-Fi斷開連接,那麼我無法訪問我的服務器。

我意識到可能有一百萬件事情是錯誤的,但我不知道從哪裏開始。我甚至不確定我應該包括哪些附加信息來幫助找到問題。如果我不得不猜測,我會說我沒有打開我的服務器端口80上的防火牆。但我真的沒有線索。 Ubuntu對我來說是新手。

+0

它的工作。我不得不在路由器上轉發端口。謝謝。 –

+0

我已將它作爲答案發布。請接受它,這樣可以關閉。 – weirdpanda

沙发
0
0

您能否提供有關您的設置的詳細信息? Ubuntu服務器如何配置?如果它是您自己的硬件,您是否轉發了路由器上的端口?你有一個動態DNS服務器嗎?是否有內部防火牆(ufw?)。 Node.js服務器是否綁定到回送? Nginx是否在proxy_pass模式下?

正如OP所述,端口未被轉發。

39
votes
answers
17 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

90
votes
answers
13 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

4
votes
answers
18 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 &quot;.../vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config/doctrine/User.orm.xml&quot; 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
25 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
36 views
+10

在Ubuntu上使用DJANGO服務NGINX

0

我試圖在Nginx服務器上運行我的django(1.8)項目,因爲它速度更快。我可以通過使用uwsgi --ini命令來設置套接字。所以我想要做的就是單獨運行NGINX想運行我的django項目,有沒有辦法做到這一點?那麼當uwsgi --ini命令結束時,由uwsgi創建的套接字將自動移除。在Ubuntu上使用DJANGO服務NGINX

NGINX config and .ini ia as shown below : 

# mysite_nginx.conf 

# the upstream component nginx needs to connect to 
upstream django { 

    server unix:///var/www/html/vir/crum/crumbs.sock; 

} 

    # configuration of the server 
    server { 
     # the port your site will be served on 
     listen  8000; 
     # the domain name it will serve for 
     server_name .localhost.com; 
     charset  utf-8; 


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

     # Django media 
     location /media { 
      alias /var/www/html/alteryx_vir/crum/media; 
     } 

     location /static { 
      alias /var/www/html/vir/crum/static; 

     } 

     # Finally, send all non-media requests to the Django server. 
     location/{ 
      uwsgi_pass django; 

      /var/www/html/vir/crum/uwsgi_params; 
     } 
    } 

    >>> uwsgi.ini file : 

    # mysite_uwsgi.ini file 
    [uwsgi] 

    # Django-related settings 
    # the base directory (full path) 
    chdir   = /var/www/html/vir/crumb/ 
    # Django's wsgi file 
    module   = crum.wsgi 
    # the virtualenv (/alteryx_vir/) 
    home   = /var/www/html/alteryx_vir/ 
    # process-related settings 
    # master 
    master   = true 
    # maximum number of worker processes 
    processes  = 10 
    # the socket (use the full path to be safe 
    socket   = 
    /var/www/html/alteryx_vir/crum/crum.sock 
    #socket   = var/run/uwsgi/crum.sock 


    # ... with appropriate permissions - may be needed 
    chmod-socket = 666 
    # clear environment on exit 
    vacuum   = true 

在此先感謝您的幫助。

沙发
0
0

是Atlast我可以通過使用UWSGI EMPEROR模式自動執行所有操作。 現在所有的命令都可以自動完成你需要做的就是啓動NGINX服務器。

Emperror模式:

編輯/etc/rc.local中並添加:

/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log 
板凳
0
0

你問的沒有意義。套接字用於nginx和uWSGI之間的通信。如果uWSGI沒有運行,那麼套接字的另一端沒有任何內容,並且沒有爲您的應用程序提供服務。

您需要nginx和uWSGI。

+0

是啊,我是小姐,然後引導thanx您的信息先生。 @丹尼爾,我怎麼能自動化。我不想運行一些命令來啓動uwsgi。希望你得到了我的觀點 –

+0

與任何服務器進程(包括nginx本身)一樣,您可以使用發行版的進程管理器 - 例如systemd或upstart - 或supervisord之類的東西。 –